Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从PHP Handled提交AJAX时,下面的代码中有什么错误?_Php_Jquery_Ajax - Fatal编程技术网

从PHP Handled提交AJAX时,下面的代码中有什么错误?

从PHP Handled提交AJAX时,下面的代码中有什么错误?,php,jquery,ajax,Php,Jquery,Ajax,我有一个名为try.php的文件,下面的代码包含所有javascript、php和html文件 <?php if(isset($_POST["submit"])){ echo "hello"; } ?> <!DOCTYPE html> <html> <head> <title>Submit Form Using AJAX and jQuery</title> <script src="http://ajax

我有一个名为
try.php
的文件,下面的代码包含所有javascript、php和html文件

<?php

if(isset($_POST["submit"])){
    echo "hello";
}

?>
<!DOCTYPE html>
<html>
<head>
<title>Submit Form Using AJAX and jQuery</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
<form method="POST" id="myForm">
    <input name="name" type="text" placeholder="Your Name">
    <input name="email" type="text" placeholder="email">
    <input name="submit" type="submit" value="Submit">
    <div id="display"></div>
</form>
<script>
$(document).ready(function(){
    $("#myForm").submit(function(event) {
        event.preventDefault(); //prevent default action 
        window.history.back();
        var form_Data = $(this).serialize();

        $.ajax({
            type: "POST",
            url: "try.php",
            data: form_data,
            cache: false,
            success:function(response){
                alert(response);
            }
        });
    });
});

</script>
</body>
</html>

使用AJAX和jQuery提交表单
$(文档).ready(函数(){
$(“#myForm”).submit(函数(事件){
event.preventDefault();//防止默认操作
window.history.back();
var form_Data=$(this).serialize();
$.ajax({
类型:“POST”,
url:“try.php”,
数据:表格数据,
cache:false,
成功:功能(响应){
警报(响应);
}
});
});
});

上述代码的目标只是提交表单,而无需简单地使用
AJAX
重新加载页面,表单数据应该由
php
处理,这里只需
回显“hello”
。上面的代码在提交时运行良好,php可以正确处理所有问题,但页面正在重新加载。代码的变化应该是什么

将其作为javascript代码进行尝试

$(document).ready(function(){
    $("#myForm").click(function(event) {
        event.preventDefault(); //prevent default action 

        var form_Data = $(this).serialize();

        $.ajax({
            type: "POST",
            url: "try.php",
            data: form_Data,
            cache: false,
            success:function(){
                alert("hello");
            }
        });
    });
});

这回答了你的问题吗?是的,在上面的代码中一切都很好,但我找不到页面重新加载的原因?存在
event.preventDefault()以防止重新加载。为什么调用window.history.back();为什么
window.history.back()?我建议解释问题所在,而不是简单地粘贴正确的代码。这样,更多的人可以从错误中学习。