Passing $post data in php

breeze351
breeze351 used Ask the Experts™
on
This may be hard to follow.
I need to pass data from page to page.  Follow me on this:
Page 1: Keyin data for main record (this is where the 1st $post data comes from)
Page 2: If  certain check box is "N", go to page 5.
    else
    Page 3:  Keyin more data.
    Page 4:  Write record and save new values for original record
Page 5: Write/Update main record

 I need to keep the $_POST data but it does not seem to work.
On page #2 when the check box is "Y", I tried
                $work = $_POST['Add_Name_1'];
                $_POST['Add_Name_1'] = $work;


But when I get to page #3, I get
Notice: Undefined index: Add_Name_1 in /home/langsyst/public_html/lansco/Contact_Insert.php on line 12
Line 12 is :
            echo "Post 1 = ".$_POST['Add_Name_1']."<br>";      

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fixer of Problems
Most Valuable Expert 2014
Commented:
Of course it doesn't work.  $_POST data is ONLY from the current page request.  No $_POST data is remembered from page to page.  However, $_SESSION data is because it is saved in a file on the server.  I use $_SESSION data a lot to remember data input on one page but needed on another.  $_SESSION data is somewhere in between $_POST and a database in it's utility.  Here's a starting point: http://php.net/manual/en/reserved.variables.session.php  Especially note the use of session_start() at the top of Every page involved.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Handling PHP forms:
http://php.net/manual/en/tutorial.forms.php

Understanding HTTP requests:
http://www.experts-exchange.com/articles/11271/Understanding-Client-Server-Protocols-and-Web-Applications.html

Passing data between HTTP requests... This is one way:
http://www.experts-exchange.com/articles/11909/PHP-Sessions-Simpler-Than-You-May-Think.html

Another, better way is to update the data model with each client input.  Each form script draws its information from the database.  Each action script writes its collected data back into the database.
http://www.experts-exchange.com/articles/12335/PHP-and-MySQLi-Table-Maintenance.html
In multipage forms, it might be worthwhile to use the $_SESSION array instead which will keep values until session_destroy() or $_SESSION = "".

OR

In the subsequent pages, you have to keep the variables preserved by using the <input type hidden> HTML tags to include the values that were passed via $_POST.  So that way they will get resubmitted to page 3 and so on.

Page 1 - form1.php
<?php
echo '
<form action="form2.php">
User's ID: <input type="text" name="ID_1" /><br>
User Name 1: <input type="text" name="Add_Name_1" /><br>
<input type="checkbox" name="certainbox" value="N" /><br>
<input type="submit" name="submit" />
</form>';
?>

Open in new window


Page 2 - form2.php
<php
if($_POST["submit"]) {
  if ($_POST["certainbox"] == "N") {
      include form5.php;
  }
  else {
      include form3.php;
  }
}
?>

Open in new window


Page 3 - form3.php
<?php
echo '
<form action="form4.php">
User name 2: <input type="text" name="Add_Name_2" /><br>
User name 3: <input type="text" name="Add_Name_3" /><br>
<input type="hidden" name="ID_1" value="'.$_POST["ID_1"].'" />
<input type="hidden" name="Add_Name_1" value="'.$_POST["Add_Name_1"].'" />
<input type="submit" name="submit" />
</form>';
?>

Open in new window


Page 4 - form4.php
<?php
# don't actually insert raw $_POST into table, cleanse it first.
#connect to your database and run update query example
$query = "
   UPDATE `tbl` SET NAME1 = \"'.mysql_real_escape_string($_POST["Add_Name_1"]). '\",
   NAME2 = \"'.mysql_real_escape_string($_POST["Add_Name_1"]). '\",
   NAME3 = \"'.mysql_real_escape_string($_POST["Add_Name_1"]). '\"
   WHERE ID =  \"'.mysql_real_escape_string($_POST["ID_1"]). '\"
";
$result = mysql_query($query);
echo 'data updated';
?>

Open in new window


Page 5 - form5.php
<?php
# don't actually insert raw $_POST variables into table, cleanse it first.
#connect to your database and run update query example
$query = "
   UPDATE `tbl` SET NAME1 = \"'.mysql_real_escape_string($_POST["Add_Name_1"]). '\"
    WHERE ID =  \"'.mysql_real_escape_string($_POST["ID_1"]). '\"
";
$result = mysql_query($query);
echo 'data updated';
?>

Open in new window

Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Most Valuable Expert 2011
Top Expert 2016

Commented:
I was in a hurry last night.   Perhaps I should add a bit of commentary to the database suggestion here:
http://www.experts-exchange.com/questions/28935602/Passing-post-data-in-php.html#a41523112

The goals seems to be twofold.  First, we need to collect some data that needs to be stored in the database.  Second, we need to use some of this data to modify the logic of the ensuing process, collecting more data, depending on whether a checkbox is checked.  Thus we have a situation that has some branching logic between pages, and that looks a little like a shopping cart design - customers can put different things into the cart, save things for later, etc.

In the early days of PHP, some programs implemented a shopping cart in the PHP session.  This was programatically easy, but it had the flaw that PHP sessions are not long-lived. If the customer did not complete the shopping activity in one sitting, the entire shopping cart was lost.   For that reason we now avoid the PHP session and put all of the data in the database.  This lets us retrieve the data at a later time, and lets the customer complete their shopping chores in a more natural manner.

The shopping cart data is connected to the client via a key stored in a long-lived http cookie.  So to take our shopping cart analogy and apply it to your application, we might change this...
Page 1: Keyin data for main record (this is where the 1st $post data comes from)
Page 2: If  certain check box is "N", go to page 5.
    else
    Page 3:  Keyin more data.
    Page 4:  Write record and save new values for original record
Page 5: Write/Update main record
... to this:
Page 1. Create an empty database record, set a cookie with the record id, then put up the form for keying in the data.
Page 2. Using the cookie id and the contents of the form from page 1, update the database.  If a certain checkbox is "N" redirect the client to page 5.
Page 3: Key in more data, on submit, go to page 4
Page 4: Using the cookie id and the contents of the form from page 3, update the database.
Page 5: Produce the appropriate response, etc.
Now you have a design where the client, if interrupted, can come back to the process and pick up where they left off.  Add a bit of garbage collection to remove empty records after some time, and you're good to go.

Author

Commented:
I knew about $_SESSION.  I was trying not to use it.
I just thought that if I get data from a post from page 1, why can't I reset it so page 2 could use it.
Glenn
Most Valuable Expert 2011
Top Expert 2016

Commented:
I knew about $_SESSION.  I was trying not to use it.
Good grief, why not?  It's as easy as pie to persist data in the session, although by now you should be aware of the risks of data loss when using the session over longer periods of time.  If you don't need long persistence, session is perfect.  If you need long persistence, then you want to use something more like this:
http://www.experts-exchange.com/articles/12335/PHP-and-MySQLi-Table-Maintenance.html

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial