Kaspars Milbergs
Kaspars Milbergs

Reputation: 784

Form method - POST delete previous data

I am trying to get some data from page to page and then mail them.

So from one form I am getting a title of item :

//Form1
<form  class="orderFormFields" method="post" action="order">
<input type="hidden" name="productName" value="<?php the_title(); ?>">
<input class="oButton" value="Order" type="submit"/>
</form>

And then is another form (next page) with other fields witch I need to mail :

<?php 
//getting a variable from previous form
$product = $_POST['productName'];

if(isset($_POST['submit'])) 
{
$name = $_POST['order_name'];
$mail = $_POST['email'];
$phone = $_POST['mobile'];
$date = $_POST['date'];
$comment = $_POST['comment'];



//simple mail function goes here

$done = true;
}

?>

//Form2 goes here

So if I insert <?php echo $product; ?> before if(isset($_POST['submit'])) I can see my variable from previous page and all works just find. But when I am inserting that same variable in mail function witch is inside if(isset($_POST['submit'])) , I cant mail that variable, seems like it is empty.

Does form method POST delete all previous form data? Because, if I change my Form1 method to GET and $product = $_POST['productName']; to $product = $_GET['productName']; I am getting that variable after Form2 submit and I can mail that variable. But I would like to prefer using POST method, because of nice URL.

Upvotes: 1

Views: 2110

Answers (1)

T1000
T1000

Reputation: 2941

You forgot to name your submit button so there is no $_POST['submit']

<input class="oButton" value="Order" type="submit" name="submit" />

EDIT: Okay, $_POST is array and have its values only after the post request. If you make another post request or change the page the previous values of $_POST are deleted and these from the new request are stored. You can store data from the first post in the sessions for example -

$_SESSION['postData']['form1'] = $_POST;

Upvotes: 1

Related Questions