Php 为什么jQueryAjax要转到url而不是加载它?
这个代码到底是怎么回事Php 为什么jQueryAjax要转到url而不是加载它?,php,jquery,ajax,Php,Jquery,Ajax,这个代码到底是怎么回事 $(".submit").click(function(){ alert("clicked"); var name = $(".name").val(); var email = $(".email").val(); var comment = $(".comment").val(); var articleId = $(".articleId").val(); var dataString = 'name='+
$(".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调用之后,我尝试使用returnfalse
,但是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;
?>
您必须阻止提交按钮的默认操作:
$(".submit").click(function(e) {
e.preventDefault();
alert("clicked");
...
});
您必须阻止提交按钮的默认操作:
$(".submit").click(function(e) {
e.preventDefault();
alert("clicked");
...
});
如果您想知道process.php是否正常工作,您需要从process.php中回显一些内容
e、 g
然后在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');
如果您想知道process.php是否正常工作,您需要从process.php中回显一些内容
e、 g
然后在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');
您是否有返回false代码>在正确的位置?i、 e在最后一组括号内。我把它放在else结尾的括号后面。。应该在哪里?那就好了。当返回false
时会发生什么情况,firebug或chrome developer工具是否会给出process.php中可能发生的任何错误?process.php在加载时会工作(切换浏览器的url),但我也没有收到成功警报只是为了确保,请尝试使用process.php的绝对url。e、 g.http://domain.com/process.php
并确保您正确访问了脚本中的变量并进行了回显。您应该能够在使用的任何开发工具中看到process.php的响应代码>在正确的位置?i、 e在最后一组括号内。我把它放在else结尾的括号后面。。应该在哪里?那就好了。当返回false
时会发生什么情况,firebug或chrome developer工具是否会给出process.php中可能发生的任何错误?process.php在加载时会工作(切换浏览器的url),但我也没有收到成功警报只是为了确保,请尝试使用process.php的绝对url。e、 g.http://domain.com/process.php
并确保您正确访问了脚本中的变量并进行了回显。您应该能够在您正在使用的任何开发工具中看到process.php的响应。嗯,我添加了这个,但是我遇到了与return false
process.php相同的问题。php不会做它应该做的事情-检查您的路径,检查您在php文件中是否使用了POST vars。。为了测试起见,试着注释掉所有内容,回显一个字符串,看看它是否正常工作,并向Ajax调用发送回复。试着在查询后回显一条成功消息,并在Firebug中查看它是否返回。如果出现错误,也会回显一条错误消息。您的$result
变量不存在。请将所有内容注释掉,然后在页面上只进行一次回显,以查看请求是否通过,以及您是否实际连接到该文件。嗯,我添加了这个,但我遇到了与返回false
process相同的问题。php不会执行它应该可以工作-检查路径,检查php文件中是否使用POST变量。。为了测试起见,试着注释掉所有内容,回显一个字符串,看看它是否正常工作,并向Ajax调用发送回复。试着在查询后回显一条成功消息,并在Firebug中查看它是否返回。如果出现错误,也会回显一条错误消息。您的$result
变量不存在,请将所有内容都注释掉,并在页面上使用1个回显,以查看请求是否通过,以及您是否实际连接到了我报告错误的文件。我直接在浏览器中转到process.php,查看它是否显示了任何错误,但没有显示任何错误。Firebug表示目前还没有回应。我真的不知道还有什么可以尝试。我有错误报告,我直接在浏览器中转到process.php,看看它是否显示了任何错误,它没有。Firebug表示目前还没有回应。我真的不知道还能尝试什么。