简体   繁体   中英

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. Within a form I have a text field and I want to retrieve that value using $ _POST within that index.php page.

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.

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.

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. Then, after you use them, you can empty the session.

You could call $("#form").submit() , after ajax is sent succesfully

$.ajax({ 
url: 'getPost.php', 
type: 'POST', 
data: value,
success: function(data){
 $('#container').html(data); 
 $('#myForm').submit();
}
});

where 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM