Passing $post data in php

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
breeze351Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
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
bigeven2002Commented:
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

HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Ray PaseurCommented:
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.
breeze351Author 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
Ray PaseurCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.