如何在$ .ajax提交时从同一页面获取带有$ _POST的表单数据

时间:2022-11-23 18:29:00

I have index.php page and within that page, there is a form. Within a form I have a text field and I want to retrieve that value using $_POST within that index.php page.

我有index.php页面,在该页面中,有一个表单。在表单中我有一个文本字段,我想在index.php页面中使用$ _POST检索该值。

The problem is I'm using JQUERY AJAX within my index.php. I also have $.ajax function. $.ajax function is to pass the data to another PHP page. If I return false in $.ajax function, the form will not submit.

问题是我在index.php中使用JQUERY AJAX。我也有$ .ajax功能。 $ .ajax函数是将数据传递给另一个PHP页面。如果我在$ .ajax函数中返回false,表单将不会提交。

So I cannot retrieve that value using $_POST. If I don't return false in $.ajax function, I can retrieve form value using $_POST within that index.php. But, $.ajax function will not work. How can I make it work both $.ajax function and $_POST to get value from that text field within that index.php.

所以我无法使用$ _POST检索该值。如果我在$ .ajax函数中没有返回false,我可以在index.php中使用$ _POST检索表单值。但是,$ .ajax函数不起作用。如何使$ .ajax函数和$ _POST同时从该index.php中的该文本字段获取值。

Thanks in advance.

提前致谢。

4 个解决方案

#1


0  

I would suggest you use session variables. When you get the POST, set SESSION as the values you want to temporarily store. Then, after you use them, you can empty the session.

我建议你使用会话变量。当您获得POST时,将SESSION设置为您要临时存储的值。然后,在使用它们之后,您可以清空会话。

#2


0  

    $(document).on("submit","form", function() {
       $.post('index.php', $("form").serialize(),function(d) {

       });
       return false;
     });

#3


0  

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. Then make a temporary form using javascript and submit that variable with that form.

2:您还可以在java脚本变量中保留文本字段的值。然后使用javascript创建一个临时表单并使用该表单提交该变量。

#4


0  

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)

#1


0  

I would suggest you use session variables. When you get the POST, set SESSION as the values you want to temporarily store. Then, after you use them, you can empty the session.

我建议你使用会话变量。当您获得POST时,将SESSION设置为您要临时存储的值。然后,在使用它们之后,您可以清空会话。

#2


0  

    $(document).on("submit","form", function() {
       $.post('index.php', $("form").serialize(),function(d) {

       });
       return false;
     });

#3


0  

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. Then make a temporary form using javascript and submit that variable with that form.

2:您还可以在java脚本变量中保留文本字段的值。然后使用javascript创建一个临时表单并使用该表单提交该变量。

#4


0  

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)