Skip to content Skip to sidebar Skip to footer

Best Approach To Add Records Into Db Using Php/ajax/mysql?

Wondering if someone can suggest a better approach to the current method I have of integrating php/js/mysql. I use this method just fine for returning sample data, setting a uniqu

Solution 1:

Here's an example of how you should do Ajax requests with jQuery/PHP/MySQL

JS

var postData = {
    first_name: 'Foo',
    last_name: 'Bar',
    phone: '1-555-432-1234'
};

$.post('path/to/ajax/url.php', postData)
 .done(function(response) {
    alert("Data Loaded: " + response);
 });

PHP

// Connect to MySQL$dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

// We want do some validation on the data so we can make a function called// `validate()` if you want.$data = validate($_POST);

$stmt = $dbh->('INSERT INTO my_table (first_name, last_name, phone) 
    VALUES (:first_name, :last_name, :phone)');

$stmt->execute($data); 

Post a Comment for "Best Approach To Add Records Into Db Using Php/ajax/mysql?"