0

I want to get post values from current page while my form action is pointing to another page. Below is code sample:

<form id="form1" name="form1" action='page2.php' method="post">
<input type="text" id="test" name="test" value="<?php echo 'test'; ?>" />
<input type='submit' value='submit' />
</form>

//i want to get the value here but it's not working because all value pass to page2.php
$value = $_POST['test'];
echo $value; 

Advise me how to perform this guys!

EDDY
  • 103
  • 1
  • 10
  • that data wouldn't exist until the form was submitted... and then you'll be on another page. not sure what you are trying to do. – Smern Mar 27 '16 at 21:12
  • change action to current page – ttdijkstra Mar 27 '16 at 21:12
  • name your script "page2.php"? – jDo Mar 27 '16 at 21:13
  • what I'm trying to do is to get the post value after I submitted the form. But I want to get the value on this page, not from the another page where the action perform. – EDDY Mar 27 '16 at 21:14
  • then change the action to the page you want it to go to? – Smern Mar 27 '16 at 21:15
  • You could save it to the session in page2 and then redirect back to page1, where you access the session item previously set. – George Kagan Mar 27 '16 at 21:15
  • @ttdijkstra i dont want to do that because I might need the data on the page where I point it to. But, I also need the data on current page as well – EDDY Mar 27 '16 at 21:15
  • perhaps a better solution would be to redesign your workflow so that you don't have this issue. Why are you trying to point the form to a different page and then trying to access it on this page? – gavgrif Mar 27 '16 at 21:16
  • @gavgrif it is because I need the data on both pages. – EDDY Mar 27 '16 at 21:17
  • So, are you saying you want to record it to a cookie or something before the page exits? I'm just curious what you're planning – 4castle Mar 27 '16 at 21:17
  • @4castle no. I would like to get the data after I click on submit. But on both pages. – EDDY Mar 27 '16 at 21:20
  • I am confused? you already have access to the form data - you simply get the value from the input value that the user has entered - you do not need to submit the form to get access to the entered values. Please elaborate as to how you need this data and why you need to submit a form to get it? – gavgrif Mar 27 '16 at 21:20
  • Still I need to submit the form for some reason. I might need to use the data to populate another dropdown or anything else. – EDDY Mar 27 '16 at 21:23

2 Answers2

1

You can intercept the form submission by using JavaScript in the onsubmit attribute of the form. Then you can make an AJAX call to your second submission location.

If you want to cancel the form submission, finish the JavaScript with return false;

Here's what it might look like:

<form method="POST" action="page2.php" onsubmit="submit(this)"> ... </form>

And then in your JavaScript:

function submit(form) {
    var ajax = new XMLHttpRequest();
    ajax.open("POST", "page1.php");
    ajax.send(new FormData(form));
}

You may find this source helpful for sending FormData objects in AJAX.

Alternatively, jQuery makes it very easy to do AJAX calls.

function submit(form) {
    $.post("page1.php", $(form).serialize());
}
4castle
  • 32,613
  • 11
  • 69
  • 106
  • This could be the answer to my problem. Just need to find a way to call it by ajax. – EDDY Mar 27 '16 at 21:25
  • Using AJAX is a separate question. There are plenty of resources for that on SO and Google, but if you want I can add a snippet of what it looks like. – 4castle Mar 27 '16 at 21:28
  • will appreciate if you can add me some idea to do it :) – EDDY Mar 27 '16 at 21:31
  • @EDDY sounds good. Okay, I've updated my answer now to wet your feet. – 4castle Mar 27 '16 at 22:14
  • Keep in mind that webpages are stateless, so once you close a page or leave it, all of the form data is forgotten unless you create a session or a cookie. Are you trying to make it so that next time the user opens the same page, there will be information from the last submit? I'm not sure if you understand how HTTP requests work. – 4castle Mar 27 '16 at 22:43
0

Here is an example using AJAX to send form data to file and execute that file without the client needing to refresh or re-direct to different pages.

function submit() {
    var v = document.getElementById("1234").value;
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
           //inserts output from file into given location
           //in this case, a div with id of 'content'              
           document.getElementById("content").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("POST", "upload.php?data="+v, true);
    xmlhttp.send(); 
}

There are ways to use JQuery also (How to submit form on file select without page reload? )

Isaac Dozier
  • 86
  • 1
  • 3
  • `POST` doesn't use the url to send data, it uses a string in the `send()` method. – 4castle Mar 27 '16 at 22:32
  • I understand but I think the URL is irrelevant to the users question. `GET` or `POST` doesn't quite matter in regards to retaining data when the goal is to work around a re-direct with Javascript/AJAX. This example came from a multimedia upload function, as to why `POST` was used. – Isaac Dozier Mar 27 '16 at 22:53
  • They aren't uploading any files. They are just submitting a form's key/value pairs. I'm saying that you need to do `xmlhttp.send("data="+v);` instead of using a query string in the url. Only `GET` sends data with the query string. [Look at the section here for using POST](http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp) – 4castle Mar 27 '16 at 22:55
  • Yes I understand how files and form data differ, and hopefully he/she understands this too and is not confused by methods. My example was only for logic. As I stated in my original post, "Here is an example using AJAX to send form data to file and execute that file without the client needing to refresh or re-direct to different pages." ...example. – Isaac Dozier Mar 28 '16 at 02:30