我希望将 html 表单发布到 php 脚本,但我不想实际将用户重定向到此脚本。我所需要的只是让脚本获取发布数据,用它做一些事情,然后以某种方式返回该数据,以便调用脚本可以使用它。这有可能吗?
我能想到的唯一方法是使用一个可以向自身发布的 php 脚本,但它非常困惑,因为我在调用脚本中使用 jQuery 和 JavaScript。
提前致谢
最佳答案
使用 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
});
关于javascript - 处理没有重定向的 PHP post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30219987/