这段代码到底是什么意思?
$(".submit").click(function(){
alert("clicked");
var name = $(".name").val();
var email = $(".email").val();
var comment = $(".comment").val();
var articleId = $(".articleId").val();
var dataString = 'name='+ name + '&email=' + email + '&comment=' + comment+ '&articleId=' + articleId;
if(name=='' || comment==''){
alert('Please Give Valid Details');
}
else{
alert('so far so good');
$.ajax({
type: "POST",
url: "../_includes/process.php",
data: dataString,
cache: false,
success: function(){
alert("succes");
$(".updating").fadeIn(400);
}
});
}
});
一切正常,直到$.ajax
找到process.php而不是读取和执行代码,它实际上会在浏览器中转到该页面。我在ajax调用之后尝试使用return false
,但是process.php中的代码永远不会发生。
这里是process.php
<?php
// code to establish connection first
if($_POST){
$name=$_POST['name'];
$name=mysql_real_escape_string($name);
$email=$_POST['email'];
$email=mysql_real_escape_string($email);
$comment=$_POST['comment'];
$comment=mysql_real_escape_string($comment);
$articleId=$_POST['articleId'];
$articleId=mysql_real_escape_string($articleId);
if(!empty($email)){
$lowercase = strtolower($email);
}
$result = mysql_query("INSERT INTO comments(name,email,comment,articleId) VALUES ('$name','$email','$comment','$articleId')");
if($result){
echo "success";
} else {
echo "there were erros" . mysql_error();
}
exit;
?>
任何帮助都将不胜感激。
答案 0 :(得分:4)
您必须阻止提交按钮的默认操作:
$(".submit").click(function(e) {
e.preventDefault();
alert("clicked");
...
});
答案 1 :(得分:1)
如果你想知道它工作正常,你需要从process.php中回复一些东西。
e.g
echo 'success';
exit; // just incase
然后在你的ajax请求中
success: function(response){
if (response == 'success') {
alert("success");
$(".updating").fadeIn(400);
}
else {
alert('error');
}
}
即使你没有回应,process.php应该仍然有用。
如果仍无法正常工作,请尝试启用错误报告:
error_reporting(E_ALL);
ini_set('display_errors', 'On');