Page 2 of 3

Posted: Tue Nov 23, 2004 4:11 pm
by wadesmart
Im very visual. If I can see what is being done Im usually a lot better. What you wrote correctly I tried - incorrectly - but now I get it.

One thing:

Code: Select all

<?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.'"

Wade

Posted: Tue Nov 23, 2004 4:16 pm
by rehfeld
you need to echo $i so php expands the variable

Posted: Tue Nov 23, 2004 7:15 pm
by wadesmart
Ok. I retyped these pages as individual pages because I was having trouble and I am still running into trouble:

Code: Select all

<?php
page one
<?php

   // check for submission
   if(!isset($_POST['submit'])) {

       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
<?php
 if(isset($_POST['step2'])) {
               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>
       ';
                       }
   }

?>

<html>
 <head>
 </head>

   <body>
     <form method = "post" action = "page3.php" >
      
       <?php echo ?>
      
       <input type = "hidden" name = "step3">
         <input type = "hidden" name = "number" value = "'.$number.'">
       <input type = "submit" name = "submit" value = "Next">
     </form>
   </body>
</html>

Page Three
<?php
    var_dump($_POST);
    $number = $_POST['number'];

   $name = $_POST["name"];
   $engine = $_POST['engine'];
   $doors = $_POST['doors'];
  
   for($i=0; $i < $number; $i++) {

     echo "<p>Car $i is a " . $_POST['name-'.$i] . " and has a " . 
$_POST['engine-'.$i] . " engine with " . $_POST['doors-'.$i] . ".</p>";

   }

   echo '<pre>';
   print_r($_POST);

?>
?>
Im getting errors on the thrid page. I added the var_dump to see what was going on and this is what I got:

Code: Select all

<?php
array(2) { ["step3"]=>  string(0) "" ["submit"]=>  string(4) "Next" }
?>
and then these are the errors:

Code: Select all

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.

Wade

Posted: Tue Nov 23, 2004 7:24 pm
by rehfeld
$_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

Posted: Tue Nov 23, 2004 7:34 pm
by wadesmart
So then I have this on page three:

Code: Select all

<?php
var_dump($_POST);

    $number = $_POST['number'];
    
    for($i=0; $i < $number; $i++) {

      echo "<p>Car $i is a " . $_POST['name-'.$i] . " and has a " . $_POST['engine-'.$i] . " engine with " . $_POST['doors-'.$i] . ".</p>";

    }

?>
I still need $number though and this also sets off errors.

Wade

Posted: Tue Nov 23, 2004 8:12 pm
by wadesmart
Ok. I made a few changes.

Code: Select all

<?php


<html>
  <head>
  </head>
  
    <body>
      <form method = "post" action = "page3.php" >
        <?php
	
  		
			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>
		    ';
			}

 
			?>
    
        
		  <input type = "hidden" name = "number" value = "'.$_POST['number'].'">
        <input type = "submit" name = "submit" value = "Next">
      </form>
    </body>
</html>


<?php

//var_dump($_POST);

    //$number = $_POST['number'];
    
    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]." .</p>";

    }
echo '<pre>';
print_r($_POST);
?>

?>
And then I get his output:

Code: Select all

<?php
Array
(
    ["name-0"] => honda
    ["engine-0"] => 2.0
    ["doors-0"] => "two"
    ["name-1"] => maxima
    ["engine-1"] => 3.0
    ["doors-1"] => "four"
    [number] => ''.$_POST[''number''].''
    [submit] => Next
)
?>
Im still not getting my echo yet.

Wade

Posted: Tue Nov 23, 2004 9:22 pm
by rehfeld
stop doing \"this\"

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.

Posted: Wed Nov 24, 2004 2:08 pm
by wadesmart
11242004 1406 GMT-6

Hooray! I got it to work. Now, applying it to my code.
I can see how this works now. I will slowly apply it to my project and see what happens.

Thanks for ALL! of your help - and patience.

Wade

Code: Select all

<?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>';

    }

?>

Posted: Fri Nov 26, 2004 3:33 pm
by wadesmart
11262004 1528 GMT-6

Ok. After playing with this for a few days I have started to change it a bit so I can use this new information on my project. Have a problem though.

The changed code:

Code: Select all

&lt;?php
&lt;?php

  if(isset($_POST&#1111;'submit1'])){
    $form_block = "
           &lt;form method = "post" action = "$_SERVER&#1111;PHP_SELF]"&gt;
    ";
    
      $number = $_POST&#1111;'number'];
        for($i=0; $i &lt; $_POST&#1111;'number']; $i++) {
            		
          $form_block .="
            &lt;fieldset&gt; 
              &lt;legend&gt;Car Number: '.$i.'&lt;/legend
              &lt;p&gt;Name: &lt;input type = "text" name = "name-'.$i.'"&gt;
              &lt;p&gt;Engine: &lt;input type = "text" name = "engine-'.$i.'"&gt;
              &lt;p&gt;Number of Doors: &lt;br /&gt;
                &lt;div&gt;&lt;input type = "radio" name = "doors-'.$i.'" value = "two"&gt;Two&lt;/div&gt;
                &lt;div&gt;&lt;input type = "radio" name = "doors-'.$i.'" value = "three"&gt;Three&lt;/div&gt;
                &lt;div&gt;&lt;input type = "radio" name = "doors-'.$i.'" value = "four"&gt;Four&lt;/div&gt;
            &lt;/fieldset&gt;
          ";
			}
    $form_block .="
        &lt;input type = "hidden" name = "number" value = "'.$number.'"&gt;
        &lt;input type = "submit" name = "submit2" value = "Next"&gt;
        &lt;/form&gt;
      &lt;/body&gt;
    &lt;/html&gt;
    ";
  }

  if(isset($_POST&#1111;'submit2'])) {
    for($i=0; $i &lt; $_POST&#1111;'number']; $i++) {
      $form_block = "
        '&lt;fieldset&gt;Car '.$i.' is a '.$_POST&#1111;'name-'.$i].' and has a '.$_POST&#1111;'engine-'.$i].' engine with '.$_POST&#1111;'doors-'.$i].' doors.&lt;/fieldset&gt;';
      ";
    }
  }
  else {
    //if(!isset($_POST&#1111;'step2'])) {
        $form_block = "
              &lt;form method = "post" action = "$_SERVER&#1111;PHP_SELF]"&gt;   
                &lt;p&gt;How many cars? &lt;input type = "text" name = "number" &gt;
                    &lt;input type = "submit" name = "submit1" value = "Go"&gt;&lt;br /&gt;
                &lt;/form&gt;
        ";
  }
?&gt;

&lt;html&gt;
  &lt;head&gt;&lt;/head&gt;
  &lt;body&gt;
    
    &lt;?php echo "form_block"; ?&gt;
    
  &lt;/body&gt;
&lt;/html&gt;

?&gt;
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?

Wade

Posted: Fri Nov 26, 2004 5:10 pm
by rehfeld
php expands variables inside double quotes, single quotes wil output a literal

Code: Select all

$i = 5;

echo "$i"; // 5
echo '$i'; // $i




echo 'foo "$i"'; // foo "$i"


echo "foo '$i'"; // foo '5'

Posted: Fri Nov 26, 2004 6:18 pm
by wadesmart
I have changed everything down to this section and nothing I do is right:

Code: Select all

<?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>;
      ";
    }

?>

Posted: Fri Nov 26, 2004 6:20 pm
by wadesmart
$form_block = "
Car '$i" is a '$_POST['name-'.$i]'
Car "$i" is a "$_POST['name....
Car is \"$i\" is a \"$_POST['name....

I just dont understand the combination.

Posted: Fri Nov 26, 2004 6:38 pm
by wadesmart
Ok, follow this logic:

Code: Select all

<?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.

Posted: Fri Nov 26, 2004 6:47 pm
by wadesmart
Ok. Went back through your posts to me and found some help but another problem arose:

Code: Select all

<?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>

?>
This works using echo but in my $form_block it only puts out * form_block* on my page.

Why?

Posted: Fri Nov 26, 2004 6:49 pm
by wadesmart
DUH!!! its $form_block