Reputation: 103
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!
Upvotes: 0
Views: 884
Reputation: 33476
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());
}
Upvotes: 1
Reputation: 86
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? )
Upvotes: 0