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, .., ):
$connection = mysql_connect($db_host, $db_user,$db_pwd);
if (!$connection){
die("Database connection failed: " . mysql.error());
}
$db_select = mysql_select_db($database, $connection);
if (!$db_select){
die("Database selection failed: " . mysql.error());
}
if (isset($_POST['submit'])) {
$name = trim(mysql_prep($_POST['name']));
$location = trim(mysql_prep($_POST['location']));
$query = "INSERT INTO user ( name, location )
VALUES ( '{$name}','{$location}' )";
$result = mysql_query($query, $connection);
}