PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
.... code before
for($i=0; $i < $_POST['number']; $i++) {
?>
<fieldset>
<p>Name: <input type = "text" name = "name-'.$i.'">
<p>Engine: <input type = "text" name = "engine-'.$i.'">
<p>Number of Doors: <br />
<input type = "radio" name = "doors-'.$i.'" value = "two">Two
<input type = "radio" name = "doors-'.$i.'" value = "three">Three
<input type = "radio" name = "doors-'.$i.'" value = "four">Four
</fieldset>
<?php
.... more code
?>
Is this not the same as echo ' code ';?
When I tried this I got exactly what you see in the source = "name-'.$i.'"
Notice: Undefined index: number in C:\WebDev\Work Directory\Zend\Arrays\page3.php on line 4
Notice: Undefined index: name in C:\WebDev\Work Directory\Zend\Arrays\page3.php on line 6
Notice: Undefined index: engine in C:\WebDev\Work Directory\Zend\Arrays\page3.php on line 7
Notice: Undefined index: doors in C:\WebDev\Work Directory\Zend\Arrays\page3.php on line 8
Ok. I reviewed your code for a long time and Im not sure why this isnt working. It looks to me though like Im not getting the variables because Im calling $doors and not $door$i what ever the number would be.
$_POST['name'] doesnt exist in the new way i showed you.
there should only be
$_POST['name-0']
$_POST['name-1']
etc.....
this should be reflected in the var dump of POST
but look at the form which submits to page 3, theres nothing in it. thats why your var dump had nothing in it.
theres no need to do this, you dont even use them anymore:
$name = $_POST["name"];
$engine = $_POST['engine'];
$doors = $_POST['doors'];
just take a look at the html your script puts out, you will see the problem right away
your in single quotes, theres not need to escape the double quotes
escape single quotes which are inside single quotes
escape double quotes which are inside double quotes
otherwise you dont need to escape them
just use it like the example i gave you
you might wanna try to use what i gave you without modifying it. get it to work, then you can rewrite your own html. changing a bunch of thigns at once make thigns harder. do 1 thing at a time, your life will be much easeir.
<?php
Page One
<?php
echo "
<html><head></head>
<body>
<form method = "post" action = "page2.php">
<p>How many cars? <input type = "text" name = "number" >
<input type = "hidden" name = "step2">
<input type = "submit" name = "submit" value = "Go"><br />
</form>
</body>
</html>
";
?>
Page Two
<html>
<head>
</head>
<body>
<?php
echo'
<form method = "post" action = "page3.php" >
';
$number = $_POST['number'];
for($i=0; $i < $_POST['number']; $i++) {
echo '
<fieldset>
<p>Name: <input type = "text" name = "name-'.$i.'">
<p>Engine: <input type = "text" name = "engine-'.$i.'">
<p>Number of Doors: <br />
<input type = "radio" name = "doors-'.$i.'" value = "two">Two
<input type = "radio" name = "doors-'.$i.'" value = "three">Three
<input type = "radio" name = "doors-'.$i.'" value = "four">Four
</fieldset>
';
}
echo'
<input type = "hidden" name = "number" value = "'.$number.'">
<input type = "submit" name = "submit" value = "Next">
';
?>
</form>
</body>
</html>
Page Three
<?php
for($i=0; $i < $_POST['number']; $i++) {
echo '<p>Car '.$i.' is a '.$_POST['name-'.$i].' and has a '.$_POST['engine-'.$i].' engine with '.$_POST['doors-'.$i].' doors.</p>';
}
?>
<?php
<?php
if(isset($_POSTї'submit1'])){
$form_block = "
<form method = "post" action = "$_SERVERїPHP_SELF]">
";
$number = $_POSTї'number'];
for($i=0; $i < $_POSTї'number']; $i++) {
$form_block .="
<fieldset>
<legend>Car Number: '.$i.'</legend
<p>Name: <input type = "text" name = "name-'.$i.'">
<p>Engine: <input type = "text" name = "engine-'.$i.'">
<p>Number of Doors: <br />
<div><input type = "radio" name = "doors-'.$i.'" value = "two">Two</div>
<div><input type = "radio" name = "doors-'.$i.'" value = "three">Three</div>
<div><input type = "radio" name = "doors-'.$i.'" value = "four">Four</div>
</fieldset>
";
}
$form_block .="
<input type = "hidden" name = "number" value = "'.$number.'">
<input type = "submit" name = "submit2" value = "Next">
</form>
</body>
</html>
";
}
if(isset($_POSTї'submit2'])) {
for($i=0; $i < $_POSTї'number']; $i++) {
$form_block = "
'<fieldset>Car '.$i.' is a '.$_POSTї'name-'.$i].' and has a '.$_POSTї'engine-'.$i].' engine with '.$_POSTї'doors-'.$i].' doors.</fieldset>';
";
}
}
else {
//if(!isset($_POSTї'step2'])) {
$form_block = "
<form method = "post" action = "$_SERVERїPHP_SELF]">
<p>How many cars? <input type = "text" name = "number" >
<input type = "submit" name = "submit1" value = "Go"><br />
</form>
";
}
?>
<html>
<head></head>
<body>
<?php echo "form_block"; ?>
</body>
</html>
?>
In my project I write code and then put it in $form_blocks so I can reuse html easily. When I tried to write the code this way I ran into a problem. You said that I had to echo things out otherwise the $i wouldnt expand. How can I get around this?
<?php
if(isset($_POST['submit2'])) {
for($i=0; $i < $_POST['number']; $i++) {
$form_block = "
<fieldset> Car "$i" is a "$_POST['name-'.$i]"
and has a ".$_POST['engine-'.'$i']." engine
with ".$_POST['doors-'.'$i']." doors. </fieldset>;
";
}
?>
<?php
if(isset($_POST['submit2'])) {
for($i=0; $i < $_POST['number']; $i++) {
$form_block = "
<fieldset>
Car '.$i.' is a '.$_POST['name-'.$i].' and has a '.$_POST['engine-'.'$i'].' engine with '.$_POST['doors-'.'$i'].' doors.
</fieldset>;
";
}
}
?>
The form puts everything inside a " " so everything inside should be ' '.
Since the form block is just a giant variable everything has to be escaped but, escaping ' ' makes no difference.