Forum Moderators: coopster

Message Too Old, No Replies

Posting Array Values across multiple pages

need help please

         

leisse

6:18 pm on Feb 17, 2005 (gmt 0)

10+ Year Member



I created an array of checkboxes, called "choices[]", and I need to pass the values from page one through to page three. Here is a sample of my code for each page:

Page One:

<input type="checkbox" name="choice[]" value="Person1">Person1
<input type="checkbox" name="choice[]" value="Person2">Person2
<input type="checkbox" name="choice[]" value="Person3">Person3
<input type="checkbox" name="choice[]" value="Person4">Person4
<input type="checkbox" name="choice[]" value="Person5">Person5

Page Two: (print the people and pass them to page three)

<?php

foreach($HTTP_POST_VARS['choice'] as $value) {
print ("<center>$value</center>\n");
}

?>

<?php

$choice = $_POST['choice'];
$nr_choices = count($_POST['choice']);

for ($i=0; $i < $nr_choices; $i++) {?>
<input type="hidden" name="choice[]" value="<? echo $_POST['choice'][$i]
;?>">
<? }

?>

Page Three: (Just display the values checked from Page One - they will be inserted to database later)

<?php

$username = $_POST['username'];
$choice = $HTTP_POST_VARS['choice'];

foreach($HTTP_POST_VARS['choice'] as $value) {
print ("<center>$value</center>\n");
}

?>

Anyone see what I did wrong on this. My values are passed from page one to page two and displayed. Getting the values to page three is the problem...Thanks.

JohnCanyon

7:28 pm on Feb 17, 2005 (gmt 0)

10+ Year Member



Seems like alot of redundant code there.. this is how I might do it.

$html = "<form ....>";
foreach($_POST[choice] as $key => $value)
{
$html .= $value;
$html .= "<input type=hidden name='choice[".$key."]' value='".$value."'>";
}
$html .= "<input type=submit...></form>";

echo $html;

Again, i believe this will accomplish the same thing, with alot less coding.

leisse

7:42 pm on Feb 17, 2005 (gmt 0)

10+ Year Member



This is pretty new to me...which page are you applying that code to, page two or page three? I'm not really sure I understand the $html variable either. Any help would be greatly appreciated.

coopster

7:58 pm on Feb 17, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, leisse.

The example being displayed seems to be for page 2. By the way, you do currently have page 2 as a <form>, correct?

leisse

8:05 pm on Feb 17, 2005 (gmt 0)

10+ Year Member



Thanks for the welcome. I do have page two as a form that does a POST to page 3(castvote.php). Page 3, for right now, just needs to post the same information that page 2 does...then i will work on getting that info into a database. Here is what page 2 looks like:

<form name = "AlumniResults" method = "post" action = "castvote.php" onSubmit="return check_id()">

<center><strong>You have chosen the following candidates:</strong></center>
<br>
<?php

foreach($HTTP_POST_VARS['choice'] as $value) {
print ("<center>$value</center>\n");
}

?>

<?php

$choice = $_POST['choice'];
$nr_choices = count($_POST['choice']);

for ($i=0; $i < $nr_choices; $i++) {?>
<input type="hidden" name="choice[]" value="<? echo $_POST['choice'][$i]
;?>">
<? }

?>

<!-- Create the Submit button and Back button -->
<center>
<input type="submit" name="vote" value="Submit Your Vote!">&nbsp;&nbsp;
<input type="button" value="Back" onClick="history.go(-1);return true;">
</center>

</form>

Choice[] is the name of the array on page one. Any ideas how to post this to page 3?

coopster

8:11 pm on Feb 17, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Why are you mixing the superglobals? In one loop you are using the long array version ($HTTP_*_VARS) and then you start using the $_POST array for the next loop. First, I would use one or the other, and preferably the newer way because the long arrays, which although are still available, are deprecated.

leisse

8:19 pm on Feb 17, 2005 (gmt 0)

10+ Year Member



I was just testing them either way. This is what code I have on page 3, but still cannot list the results:

<?php

$choice = $HTTP_POST_VARS['choice'];

foreach($HTTP_POST_VARS['choice'] as $value) {
print ("<center>$value</center>\n");
}

?>

To be honest, I was just trying out different ways. I'm really struggling with getting page one's checkbox array values all the way through to page 3.

coopster

2:59 am on Feb 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



A quick way that I will test variables on any given page is to dump them all at the very beginning of the script with the print_r() [php.net] function. So, at the top of your page 3 script, do something like this...
print '<pre>'; 
print_r($_POST);
print '</pre>';
exit;

This will dump all the POSTed form variables to the browser and terminate the script processing. At least you will be able to see if the 3rd page is getting your variables or not.

leisse

2:01 pm on Feb 18, 2005 (gmt 0)

10+ Year Member



Thanks Coopster,
This is the result of my third page:

Array
(
[choice] => Array
[username] => hhh
[vote] => Submit Your Vote!
)

I am passing a username and the submit button on page 2. It just looks like the Array is not being passed still. Any suggestions?