处理PHP帖子没有重定向

时间:2015-05-13 16:02:18

标签: javascript php jquery

我希望将一个html表单发布到php脚本,但我不想将用户重定向到此脚本。我需要的只是让脚本获取post数据,用它做一些事情然后以某种方式返回这些数据,这样它就可以用于调用脚本。这有可能吗?

我能想到的唯一方法就是拥有一个发布到自身的php脚本,但是因为我在调用脚本中使用了jQuery和JavaScript,所以它非常混乱。

提前致谢

1 个答案:

答案 0 :(得分:1)

使用JQuery Ajax。

假设你有一个像

这样的表格
<form id="Form" action="response.php" method="post">...</form>

JQuery的:

$('#Form').submit(function (event){
        $.ajax({
            type:"POST", // define method post or get
            url:$('#Form').attr('action'), // gets the post url from your action attribute
            data:$('#Form').serialize(), // binds your form data
            dataType:'json', // server response data type, use 'html' 'json' etc
            encode:true,
            success:function(data){
            },
            error:function( xhr, status, errorThrown ) {
            alert( "Sorry, there was a problem!" );
            console.log( "Error: " + errorThrown );
            console.log( "Status: " + status );
            console.dir( xhr );

            },
        });
         event.preventDefault(); // prevents from going to the posting url
    });
相关问题