Unstoppable Domains

Passing Info W/ Forms & PHP Any Ideas?

Spaceship Spaceship
Watch

Archangel

randypendleton.comTop Member
Impact
1,774
I'm building a script in PHP that has 3 parts, spanning 3 pages. The first 2 were easy. But I'm kinda baffled at the third.

The script builds a form, according to how many elements the user specifies. Then, it displays the results on a webpage. If this sounds German to you, just read on.

The first page is a simple drop-down box, asking how many elements the form requires. Let's say a user chooses 5. After pressing submit, the user goes to page 2.

On this page, the actual form displays, showing text boxes. We're assuming the user chose 5 so this page has 5 boxes. I should note that I passed the variable on to the 3rd page as well, the one specifying the number 5, in this example.I used a for() to add the form elements. I was able to append the names I gave each element; see below:

($number is the var for the drop-down box at the very beginning)

<?php
for ($i=1; $i<=$number; $i++)
{
echo "Name For Member " . $i .":ย  <INPUT TYPE=text NAME=name".$i . " SIZE=30><br>";
} ?>

Once the form is complete, the user presses submit and goes to the final page, the one that displays the user's input. THIS is the tricky part...

...how would I do this?

I passed the initial var from the first part to the second & then to the third. But now I'll need to be able to add:

$name = $_POST["name"];

But 'name' is affixed with a number ie With 5 in mind, all 5 text boxes are named name1, name2, name3 etc. I don't think I could use ["name" + 1] in this situation...

I think I'd need an array but I dunno how I'd set this up.

How do I do this? :p
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
.US domains.US domains
Okay this is how I would do it.
First I would also pass an invisible input with the value of the amount of numbers.

So your second page's form would look like so:
Code:
<form method="post">
<?php
 $number = $_POST['number'];
for ($i=1; $i<=$number; $i++)
{
?>
<strong>Name For Member #<?php echo $i;?>:</strong> <input type="text" name="name<?php echo $i;?>" size="30" /><br />
<?php } ?>
<input type="hidden" name="number" value="<?php echo $number;?>" />
<input type="submit" />
</form>

And on your next page you could say something like this:
Code:
<?php 
$number = $_POST['number'];
for ($i=1; $i<=$number; $i++)
{
?>
<strong>Name for Member #<?php echo $i;?>:</strong> <?php echo $_POST['name'.$i];?><br />
<?php } ?>

Okay....so what this does, on the first on you pass the persons number he choose to the 3rd page...so you know how many 'Name for Member' strings you must pass in the for loop. And then you just echo your values in any way you want with $_POST['name'.$i] being the same thing as $_POST['name2'] (or three or four, etc......). If none of that made sense.....well just let me know. I hope it helped cause I just learned a lot of PHP and wouldn't have been able to do it a month ago. I love to help :)
-Sam
 
1
•••
This is very helpful -- I'll test this out the first chance I get. Big reps are coming your way -- thanks:)


Okay this is how I would do it.
First I would also pass an invisible input with the value of the amount of numbers.

So your second page's form would look like so:
Code:
<form method="post">
<?php
 $number = $_POST['number'];
for ($i=1; $i<=$number; $i++)
{
?>
<strong>Name For Member #<?php echo $i;?>:</strong> <input type="text" name="name<?php echo $i;?>" size="30" /><br />
<?php } ?>
<input type="hidden" name="number" value="<?php echo $number;?>" />
<input type="submit" />
</form>

And on your next page you could say something like this:
Code:
<?php 
$number = $_POST['number'];
for ($i=1; $i<=$number; $i++)
{
?>
<strong>Name for Member #<?php echo $i;?>:</strong> <?php echo $_POST['name'.$i];?><br />
<?php } ?>

Okay....so what this does, on the first on you pass the persons number he choose to the 3rd page...so you know how many 'Name for Member' strings you must pass in the for loop. And then you just echo your values in any way you want with $_POST['name'.$i] being the same thing as $_POST['name2'] (or three or four, etc......). If none of that made sense.....well just let me know. I hope it helped cause I just learned a lot of PHP and wouldn't have been able to do it a month ago. I love to help :)
-Sam
 
0
•••
Here is how I solve that:

PHP:
<?php
for ($i=1; $i<=$number; $i++)
{
echo "Name For Member " . $i .":ย  <INPUT TYPE=text NAME=name[] SIZE=30><br>";
} ?>

On the 3rd (last) page:

PHP:
<?php

$name = array();
$name = $_POST['name'];  // or $_GET, whichever you have it set

foreach ($name as $i){
echo "$i"; // whatever your output is here. 
}

?>

That *should* work, but haven't tested it.

You want to turn each form element into an array by naming it w/ brackets:

something[]

Then, to display everything (no matter the number), declare the variable an array:

$name = array();

The foreach() part takes the array and runs it until all data has been shown.
 
Last edited:
1
•••
Thanks to you both -- it was appreciated. I repped you both. :)
 
Last edited:
0
•••
Glad I could actually help.
 
0
•••
Nevermind. I used both of your guys' codes and experimented. I got it working perfectly. :)
 
Last edited:
0
•••
Yay!
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Appraise.net
Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back