Avatar of breeze351
breeze351
 asked on

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
PHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Dave Baldwin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

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

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

Passing data between HTTP requests... This is one way:
https://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.
https://www.experts-exchange.com/articles/12335/PHP-and-MySQLi-Table-Maintenance.html
bigeven2002

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

Ray Paseur

I was in a hurry last night.   Perhaps I should add a bit of commentary to the database suggestion here:
https://www.experts-exchange.com/questions/28935602/Passing-post-data-in-php.html?anchorAnswerId=41523112#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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
breeze351

ASKER
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 Paseur

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:
https://www.experts-exchange.com/articles/12335/PHP-and-MySQLi-Table-Maintenance.html