Reputation: 9408
I usually work with .NET but I have a php page that calls an ajax post through another php script with parameters, then calling a stored procedured. But it does not work when trying to write to the mysql database. Can anyone tell me what I am doing wrong, Thanks for any help.
jquery call
$(document).ready(function () {
$('#ValidateTest').hide();
$('#cf_submit').click(function () {
if ($('#cf_message').val() == '' || $('# cf_name').val() == '') {
$('#ValidateTest').html('Please complete.').css({ 'color': 'red' }).show();
return;
}
var parameters = {
'name': $('#cf_name').val(),
'message': $('#cf_message').val()
}; //Use JSON to pass parameters into ajax calls
//Make ajax call to post to database
$.ajax({
type: 'POST',
url: 'SendTest.php',
datatype: 'json',
data: parameters,
success: function () {
$('#ValidateTest').html('Thank-you!').css({ 'color': 'green' }).show();
}
});
}); //End button click
}); //end jquery call
php script
<?php
$con = mysql_connect('host', 'username', 'passw');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("", $con);
mysql_query("CALL sp_CreateTestimony("$_POST['message']", "$_POST['name']")");
mysql_close($con);
?>
Upvotes: 0
Views: 731
Reputation: 1451
You are making the ajax request using json. so firts you have to decode the json
$data = json_decode($_POST['data']);
Then you can access message and name using $data array.
Upvotes: 1