简体   繁体   English

$ .ajax提交表单时,如何从同一页面使用$ _POST获取表单数据

[英]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.

相关问题 提交表单时如何将值传递给同一页? 使用Ajax - How to pass a value to the same page when a form submitted? Using Ajax 如何在$ .ajax方法提交的表单中使用$ _POST方法获取数据 - how to get data using $_POST method when form submitted by $.ajax method 当同一页面上有多个表单时,如何使用 AJAX 从一个表单发布数据? - How to POST data using AJAX from one form when there are multiple forms on the same page? 提交表单时如何获取从另一个页面传递的变量 - How to get the variable passed from another page when form is submitted 在同一页面中提交表单时显示ajax成功或错误消息 - show ajax success or error message when form is submitted in the same page 如何在同一页面上显示来自 WSForm Wordpress 插件表单的 AJAX 发布数据? - How to show-up AJAX post data from WSForm Wordpress plugin form on the same page? 从另一个表单添加数据时,$ _ POST / $ _ GET与在同一页面上提交数据的$ _POST / $ _ GET - $_POST/$_GET with that submits data on the same page gets erased when adding data from another form jQuery Ajax Post-表单数据未正确提交 - jQuery Ajax Post - Form data is not submitted correctly 如何从使用AJAX提交的表单(与其他具有相同类名的表单)中选择和提取数据? - How to select and extract data from a form that is submitted with AJAX which is among other forms with the same class name? 如何从一页发布表单数据并在同一页面上检索? - How to post form data from one page and retrieve it on the same page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM