如果您使用 jQuery,您可以对服务器端脚本进行 AJAX 调用,并使用成功回调在客户端启动对话。
$.ajax({
url: 'ajax/test.php',
data: { name: "WeLikeThePandaz", email: "panda@gmail.com" },
success: function(response) {
if (response.status == "OK"){
// Show dialog
}else{
// Let the user know there were errors
alert(response.error);
}
}
},'json');
这是使用该$.ajax
方法的相关文档 -
http://api.jquery.com/jQuery.ajax/
然后,您的服务器端 PHP 代码ajax/test.php
可以破译发送的数据并组装一个 json 对象以返回给 jQuery -
<?php
$err= '';
$name = sanitizeString($_POST['name']);
$email = sanitizeString($_POST['email']);
// note the sanitization of the strings before we insert them - always make sure
// to sanitize your data before insertion into your database.
// Insert data into database.
$result = mysql_query('INSERT INTO `user_table` VALUES...');
if (!$result) {
$status = "FAIL";
$err = mysql_error();
}else{
$status = "OK";
}
echo json_encode(array('error'=>$err,'status'=>$status)); // send the response
exit();
?>