我通常使用.NET,但我有一个 php 页面,它通过另一个带参数的 php 脚本调用 ajax post,然后调用存储过程。但是当尝试写入mysql数据库时它不起作用。谁能告诉我我做错了什么,谢谢您的帮助。
jquery调用
$(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 脚本
<?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);
?>
最佳答案
您正在使用 json 发出 ajax 请求。所以首先你必须解码 json
$data = json_decode($_POST['data']);
然后您可以使用 $data 数组访问消息和名称。
关于PHP、jquery ajax 调用不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8015047/