Hi guys i'm building a booking system on my website. Now i have list of the equipment, which i'm obviously displaying along with small input object, where people can type in quantity of each peace of kit they want.

<input type=\"hidden\" name=\"n$id\" value=\"$name\" /><input type=\"text\" name=\"quantity$id\" size=\"1\" />

then on the next step i have

do {

if($$quantity == "") { }
else {

echo $$quantity . ' x ';
echo $$gear_name . '<br />'; }

$n++;	} while ($n <= $max); 

So after filling quantity, people can see what order they've just placed. Now i want to send that with the email to the myself, but this:

$message .= " $$quantity x $$gear_name ";

doesn't work. So i can't do anything other than displaying with echo that 2 variable variables. How to do this anybody knows ? If you have any ideas i would appreciate

Thanks a lot

    I would suggest separating text from variables manually (always)
    does this work?

    message = $$quantity . 'x' . $$gearname;

      I would suggest a different format entirely. Change the HTML to this:

      <input type="hidden\" name=\"n[$id]\" value=\"$name\" /><input type=\"text\" name=\"quantity[$id]\" size=\"1\" />

      so that $POST['n'] and $POST['quantity'] are arrays. Use a [man]foreach/man and use the $id => $val structure to loop through all submitted values.

      If variable variables is the answer, usually you asked the wrong question; they can often be replaced with arrays and the code is simplified.

        thanks for the idea !

        but .. i can't really get my head around it .. i tried that:

        $gear_name = array($_POST['n']);
        foreach ($gear_name as $id => $_POST['n']) {
        
        echo("$gear_name");
        
        }
        

        but it neither doesn't make sense or work πŸ˜‰ what am i doing wrong ?

        thanks

          elemelas wrote:

          what am i doing wrong ?

          If you modified your HTML form elements in the manner I suggested above, then $POST['n'] is already an array, so by doing array($POST['n']) you're essentially placing an array inside of an array.

          Try doing a [man]print_r/man on $_POST['n'] to see a visual representation of how the data is presented when you use the array notation in the HTML form.

            if $_POST['n'] is an array then you get all its elements like so:

            foreach ($_POST['n'] as $key=>$value) {
                echo $key . '=>' . $value;
            }

              yep, i've changed the form exactly the way you suggested.

              print_r ($_POST['n']);

              doesn't do anything .. :/

                Can you show us the HTML output for the form? Also, you did specify that "post" is to be used as the <form> method in the HTML code, correct?

                  Bjom;10918891 wrote:

                  if $_POST['n'] is an array then you get all its elements like so:

                  foreach ($_POST['n'] as $key=>$value) {
                      echo $key . '=>' . $value;
                  }

                  am i interpreting this right, would $key be $id and value $n ? i've never operated on arrays sorry guys for stupid questions :/

                    here you go:

                    <form method=\"post\" action=\"book.php?action=pa&step=1\" name=\"book\"><input type=\"hidden\" name=\"n[$id]\" value=\"$name\" /><input type=\"text\" name=\"quantity[$id]\" size=\"1\" /> $name

                      Is that what's actually outputted to the browser, e.g. if you viewed the source of the page in your browser then that is the exact code you would see? If so, then you shouldn't have all of those backslashes in there.

                      elemelas wrote:

                      am i interpreting this right, would $key be $id and value $n ?

                      Yes; the foreach() would loop through each array item and store the current key value into $key and the corresponding value in $value. In code like:

                      $foo[0] = 'bar';

                      0 is the key (or index) and bar is the value for that array item.

                      You can see more info/example in the manual: [man]foreach[/man] and [man]array[/man].

                        As Brad said, follow the links and read through the manual. Plus: make an array yourself, and experiment with it, before you continue with your project. Experiment a lot. Arrays are important.

                          bradgrafelman;10918902 wrote:

                          Is that what's actually outputted to the browser, e.g. if you viewed the source of the page in your browser then that is the exact code you would see?

                          Sorry, i didn't realised that you wrote output. Thats source of first entry:

                          <form method="post" action="book.php?action=visual&step=1" name="book"><input type="hidden" name="n1" value="19'' LCD TFT Screen" /><input type="text" name="quantity1" size="1" /> 19'' LCD TFT Screen

                            That code still shows the old name="n1" method you were using instead of the suggested name="n[1]" method.

                              and thats the loop i was trying to create:

                              foreach ($_POST['n'] as $n=>$id) {
                                  echo $n . '=>' . $id;
                              
                              } 

                              but it doesnt work ..

                              BTW why there's no post EDIT option on this forumπŸ˜‰

                                bradgrafelman;10918911 wrote:

                                That code still shows the old name="n1" method you were using instead of the suggested name="n[1]" method.

                                actually not,because id of that entry is 1, the next one is:

                                <input type="hidden" name="n2" value="20'' LCD TV w/built in DVD Player" /><input type="text" name="quantity2" size="1" /> 20'' LCD TV w/built in DVD Player

                                so i presume its ok ?

                                  or maybe not, n[1] & n[2] should be outputted shouldn't be ?

                                  why is that .. ?

                                    elemelas wrote:

                                    BTW why there's no post EDIT option on this forum

                                    That's part of a new-member/SPAM protection setting (the edit feature is disabled along with several other forum features/abilities) - you'll automatically get upgraded to a full member after a certain (very small) number of posts.

                                    elemelas wrote:

                                    so i presume its ok ?

                                    Nope - the HTML code in your browser must read n[1] so that when the form is submitted, PHP will see the brackets and automatically convert the separate POST'ed elements into one array.

                                    This is explained in the PHP manual here: [man]variables.external[/man] (scroll down to "Example #3 More complex form variables").

                                    elemelas wrote:

                                    or maybe not, n[1] & n[2] should be outputted shouldn't be ?

                                    why is that .. ?

                                    As I said above, yes, you should see the [] brackets in the HTML code sent to your browser.

                                    As for why they aren't showing up... can you copy and paste the exact PHP code you have that generates the HTML output for the form?

                                      here you go brad

                                      if($action=="visual") {
                                      
                                      if($step=="") {
                                      
                                      echo("<form method=\"post\" action=\"book.php?action=visual&step=1\" name=\"book\"><table>");
                                      
                                      echo("<tr><td>LCD/Plasma Screens</td></tr>"); 
                                      
                                      $query = mysql_query("SELECT * FROM equipment WHERE type='LCD/Plasma Screens' ORDER BY id");
                                       while ($row = mysql_fetch_object($query)) {
                                      
                                         $id=$row->id;
                                         $name=$row->name;
                                      
                                      echo("<tr><td><input type=\"hidden\" name=\"n$id\" value=\"$name\" /><input type=\"text\" name=\"quantity$id\" size=\"1\" /> $name</td></tr>");
                                      
                                      }
                                      
                                      echo("</table><br /><input type=\"submit\" name=\"Submit\" value=\"Book !\" /></form>");
                                      
                                      }

                                        sorry that was the old code, that the new one:

                                        if($action=="visual") {
                                        
                                        if($step=="") {
                                        
                                        echo("<form method=\"post\" action=\"book.php?action=visual&step=1\" name=\"book\"><table>");
                                        
                                        echo("<tr><td>LCD/Plasma Screens</td></tr>");
                                        
                                        $query = mysql_query("SELECT * FROM equipment WHERE type='LCD/Plasma Screens' ORDER BY id");
                                        while ($row = mysql_fetch_object($query)) {
                                        
                                           $id=$row->id;
                                           $name=$row->name;
                                        
                                        echo("<tr><td><input type=\"hidden\" name=\"n[$id]\" value=\"$name\" /><input type=\"text\" name=\"quantity[$id]\" size=\"1\" /> $name</td></tr>");
                                        
                                        }
                                        
                                        echo("</table><br /><input type=\"submit\" name=\"Submit\" value=\"Book !\" /></form>");
                                        
                                        } 

                                        sorry my mistake