Jump to content

Passing array data from page to page to page


CincoPistolero

Recommended Posts

Hi Everyone,

I have a simple form that needs to capture form data and pass it over a couple pages before updating a database. I am not using sessions, nor does my client want to at this time.

 

I can pass the basic <input type=text> data easily from page to page. My issue is I have two sets of checkboxes that I create from a db table. The user can select multiple checkboxes and it saves to sOpt[]. I need to know how I can pass that array over two pages and then update the db on the fourth page. Below is a simplified version of what I'm trying to do.

 

Page1.php

<form method=post action="page2.php">
<input type="text" name="first">
<input type="text" name="last">
<input type="submit" value="next">
</form>

 

page2.php

<head>
<?php $first = $_POST['first'];
$last = $_POST['last'];
?>
</head>
<form method="post" action="page3.php">
<input type="hidden" name="first" value="<?php $first; ?>">
<input type="hidden" name="first" value="<?php $last; ?>">
<!-- this code displays the checkboxes, I don't have any issues with this code -->
<?php
$max2=2; 
                $query2="SELECT op.optPricesID, o.name FROM optPrices op, options o WHERE op.optionID = o.optionID AND op.drumTypeID=3"; 
                $result2=@mysql_query($query2); 
                $num2=mysql_num_rows($result2); 
                 
                if(empty($num2)){ 
                    $final2="\t<tr><td><center><span class='optTitles'>Nothing to Display</span></center></td></tr>\n"; 
                }else{ 
                    while($row2=mysql_fetch_array($result2,MYSQL_NUM)){ 
                        $array2[]="<input type=checkbox name='sOpt[]' value=".$row2[0]."\">".$row2[1]; 
                    } 
                     
                mysql_free_result($result2); 
                 
                    $final2="<tr>\n"; 
                     
                    foreach($array2 as $thumbnail2){ 
                    if($counter2==$max2){ 
                        $counter2=1; 
                        $final2.="\n<tr>\n"; 
                    }else $counter2++; 
                        $final2.="\t<td align=\"left\" width=\"270\" height=\"20\"><span class='optTitles'>".$thumbnail2."</span></td>\n"; 
                    }                      
                        if($counter2){ 
                            if($max2-$counter2){ 
                                $final2 .= "\t<td width=\"270\" height=\"20\"> </td>\n";  
                            } 
                            $final2.="</tr>"; 
                            } 
                        } 
					echo " $final2"; 
?>

<input type="submit" value="next">
</form>

 

page3.php - this is where I get confused - This pages displays choices

<head>
<?php
$first = $_POST['first'];
$last = $_POST['last'];
[b]...I'm not sure how to declare the array...[/b]
</head>
<form method="post" action="page4.php">
<input type="hidden" name="first" value="<?php $first; ?>">
<input type="hidden" name="first" value="<?php $last; ?>">
[b]...need to know how to declare the ARRAY for sending to next page...[/b]

First Name:  <?php echo $first; ?>
Last Name:  <?php echo $last; ?>
OPtions: [b]...need to know how to list options here...[/b]
<input type="submit" value="submit">

 

page4.php

I'm pretty confident on how to insert into the db as long as I get the array setup correctly on page3.php

 

thanks for any and all help.

Link to comment
Share on other sites

I would suggest using array names for your inputs on the first page, like this:

<input type="text" name="firstpage[first]">
<input type="text" name="firstpage[last]">
... and so on...

Then just use a loop to create your hidden inputs on the second page:

<?php
foreach ($_POST['firstpage'] as $name=>$value){
  echo "<input type='hidden' name='$name' value='$value'>";
}
?>

Link to comment
Share on other sites

OK, let's say this is the first page. In your code, you have two inputs named "first" and "last." So, using that example, I changed it to this:

 

Page1.php

<form method=post action="page2.php">
<input type="text" name="firstpage[first]"> <!-- using "firstpage[first]" as the name in place of "first" -->
<input type="text" name="firstpage[last]"> <!-- similar name change -->
<input type="submit" value="next">
</form>

 

Now just use a loop to create your hidden inputs on the second page:

 

page2.php

<form method="post" action="page3.php">
<?php // this will grab all input values from anything named firstpage[*] and create hidden inputs with those names and values
foreach ($_POST['firstpage'] as $name=>$value){
  echo "<input type='hidden' name='$name' value='$value'>";
}
?>

// ...the rest of your page2.php page here...

Link to comment
Share on other sites

this did not work. Only the first checkbox displayed on page3.php

 

page2.php - this brings my checkboxes in from the database

if(empty($num)){ 
                    $final="\t<tr><td><center><span class='optTitles'>Nothing to Display</span></center></td></tr>\n"; 
                }else{ 
                    while($row=mysql_fetch_array($result,MYSQL_NUM)){ 
                        $array[]="<input type=checkbox name='firstoptions[tomBassOpt]' value=".$row[0]."\">".$row[1]; 
                    } 

 

page3.php - with below code it only displays one checkboxes info

foreach ($_POST['firstoptions'] as $name=>$value) {
     			 echo    "$name   $value<br>";
   			}

 

 

Once again, for anyone new. I'm pulling data from a table that lists a dozen or so checkboxes with the same name(page2.php). I want to pass this array to a confirm page and display it(page3.php) then pass it again to a fourth page that will upload the array into a table(page4.php)

Link to comment
Share on other sites

this is the first phase of an order form and he does not want the users to have to login. Can sessions be used without a login?

 

Also, I don't want to send the array as a string into one row in a table. I want the array to create a new row for every option selected.

Link to comment
Share on other sites

Of course sessions can be used without a login.  As far as my serialize comment, I wasn't suggesting you store the data permanently that way.  Just from form page to form page and then on your final page, retrieve the data, unserialize it, sanitize it, do error checking and store the final results in your database however you would like.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.