[英]How to get form data with $_POST from same page when it is submitted by $.ajax
I have index.php page and within that page, there is a form. 我有index.php页面,并且在该页面中,有一个表单。 Within a form I have a text field and I want to retrieve that value using $
_POST
within that index.php page. 在表单中,我有一个文本字段,我想在index.php页面中使用$
_POST
检索该值。
The problem is I'm using JQUERY AJAX within my index.php . 问题是我在index.php中使用了JQUERY AJAX。 I also have
$.ajax
function. 我也有
$.ajax
函数。 $.ajax
function is to pass the data to another PHP page. $.ajax
函数是将数据传递到另一个PHP页面。 If I return false in $.ajax
function, the form will not submit. 如果我在
$.ajax
函数中返回false,则该表单将不会提交。
So I cannot retrieve that value using $_POST
. 因此,我无法使用
$_POST
检索该值。 If I don't return false in $.ajax
function, I can retrieve form value using $_POST
within that index.php . 如果我没有在
$.ajax
函数中返回false,则可以在该index.php中使用$_POST
检索表单值。 But, $.ajax function will not work. 但是,$。ajax函数将不起作用。 How can I make it work both
$.ajax
function and $_POST
to get value from that text field within that index.php. 我如何使它既可以使用
$.ajax
函数又可以使用$_POST
来从该index.php中的文本字段获取值。
Thanks in advance. 提前致谢。
$(document).on("submit","form", function() {
$.post('index.php', $("form").serialize(),function(d) {
});
return false;
});
I would suggest you use session variables. 我建议您使用会话变量。 When you get the POST, set SESSION as the values you want to temporarily store.
收到POST时,将SESSION设置为要临时存储的值。 Then, after you use them, you can empty the session.
然后,在使用它们之后,您可以清空会话。
You could call $("#form").submit()
, after ajax is sent succesfully 您可以在成功发送ajax之后调用
$("#form").submit()
$.ajax({
url: 'getPost.php',
type: 'POST',
data: value,
success: function(data){
$('#container').html(data);
$('#myForm').submit();
}
});
where value = {name: "noname"} // javascript object(json)
其中
value = {name: "noname"} // javascript object(json)
You can do it with different approaches like 您可以使用不同的方法来做到这一点
1: You can have a hidden field and form then populate that field with the same value of text field while sending ajax call and then submit that form after completion of ajax call 1:您可以具有隐藏字段和表单,然后在发送ajax调用时使用文本字段的相同值填充该字段,然后在ajax调用完成后提交该表单
2: You can also preserve the value of text field in a java script variable. 2:您还可以在Java脚本变量中保留文本字段的值。 Then make a temporary form using javascript and submit that variable with that form.
然后使用javascript制作一个临时表格,并使用该表格提交该变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.