How to transfer data from jQuery to php to save to database?

I read about the jQuery.ajax method and believe that this should be what I need, but cannot make it work yet.

I created a mysql test database with the users table, adding rows to this table for the name and location, and then made sure that I could save the data to it using the command line, which I could do. Then I made a test page with a button on it and added this copy to my script file (part of $ .ajax is executed directly from jQuery api examples):

$('#button').click(function(){
    saveData();
});

function saveData(){  
$.ajax({
       type: "POST",
   url: "process.php",
   data: { name: "John", location: "Boston" }
    }).done(function( msg ) {
 alert( "Data was saved: " + msg );
    });
}

" ", . - process.php, , . process.php( , db_host, .., ):

// 1. Create a connection to the server. 
$connection = mysql_connect($db_host, $db_user,$db_pwd);

// make sure a connection has been made
if (!$connection){
die("Database connection failed: " . mysql.error());
}

// 2. Select the database on the server
$db_select = mysql_select_db($database, $connection);
if (!$db_select){
die("Database selection failed: " . mysql.error());
}

// START FORM PROCESSING
if (isset($_POST['submit'])) { // Form has been submitted.
    $name = trim(mysql_prep($_POST['name']));
$location = trim(mysql_prep($_POST['location']));

// INSERT THE DATA 
$query = "INSERT INTO user ( name, location )
              VALUES ( '{$name}','{$location}' )";
        // Confirm if the query is successful.
        $result = mysql_query($query, $connection);
}
+3
6

, "" "", /. , . , Ajax .

, , , :

data: { name: "John", location: "Boston" }

PHP- , ​​ :

if (isset($_POST['submit']))

, , , , , - , "data" Ajax. :

if ( $_SERVER['REQUEST_METHOD'] == 'POST' )
+3

$_POST ['submit'] , , , rsendin ajax, , "submit".

:

if ((isset($_POST['name'])) && (isset($_POST['location']))) { // Form has been submitted.
    $name = trim(mysql_prep($_POST['name']));
$location = trim(mysql_prep($_POST['location']));

// INSERT THE DATA 
$query = "INSERT INTO user ( name, location )
              VALUES ( '{$name}','{$location}' )";
        // Confirm if the query is successful.
        $result = mysql_query($query, $connection);
}
+1

"" "". :, , , :

success: function(data)
    {
       alert(data);
    },
error:function (xhr, ajaxOptions, thrownError){
        alert("Error Status: " + xhr.status + " Thrown Errors: "+thrownError);
    }
0

,

.done(function( msg ) {
     alert( "Data was saved: " + msg );
});

, ajax . , .success. , , , , , msg - php, - . (php , .Net, JS:))

0

. submit process.php, DB

$('#button').click(function(){
    saveData();
});

function saveData(){  
$.ajax({
       type: "POST",
   url: "process.php",
   data: { name: "John", location: "Boston",submit:"submit" }
    }).done(function( msg ) {
 alert( "Data was saved: " + msg );
    });
}
0

, ajax, , submit . , else . die, if

if (isset($_POST['submit'])) { // Form has been submitted.
    $name = trim(mysql_prep($_POST['name']));
$location = trim(mysql_prep($_POST['location']));

// INSERT THE DATA 
$query = "INSERT INTO user ( name, location )
              VALUES ( '{$name}','{$location}' )";
        // Confirm if the query is successful.
        $result = mysql_query($query, $connection);
} else {
  die("$_POST['submit'] didn't exist");
}
0
source

All Articles