Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Mind you, this is just something quick I typed up in about 10 mins, so I'm sure there is a better way to do this, but, it gets the job done.</p> <p>PHP:</p> <pre><code>if(isset($_POST['submit'])) { $to = "test@websitehere.com"; $subject = "Form Tutorial"; $name_field = $_POST['name']; $email_field = $_POST['email']; $order = array(); foreach($_POST['item'] as $item =&gt; $name) { if ($_POST['quantity_'.$name] &gt; 0) { $add_order = array('pretty'=&gt;$_POST['pretty-name_'.$name],'qty'=&gt;$_POST['quantity_'.$name],'message'=&gt;$_POST['message_'.$name]); array_push($order,$add_order); } } $body = "From: $name_field\nE-Mail: $email_field\n"; $body .= "Their Order:\n"; foreach ($order as $item){ $body .= "--".$item['qty']."x ".$item['pretty']."\n Extra: ".$item['message']."\n\n"; } echo "Data has been submitted to $to!"; mail($to, $subject, $body); } </code></pre> <p>HTML:</p> <pre><code>&lt;form action="test.php" method="post"&gt; &lt;div class ="item_left"&gt; Mexican Torta - $8.50&lt;input name="item[]" type="hidden" value="torta"/&gt; &lt;input name="pretty-name_torta" type="hidden" value="Mexican Torta"/&gt;&lt;br&gt; How Many? &lt;input name="quantity_torta" type="text" /&gt;&lt;br&gt; &lt;input name="message_torta" type="text" value="Enter special order instructions here..." /&gt; &lt;/div&gt;&lt;!-- ITEM_LEFT --&gt; &lt;br /&gt; &lt;div class ="item_center"&gt; Fish Sandwich - $8.50&lt;input name="item[]" type="hidden" value="fish"/&gt; &lt;input name="pretty-name_fish" type="hidden" value="Fish Sandwhich"/&gt;&lt;br&gt; How Many? &lt;input name="quantity_fish" type="text" /&gt;&lt;br&gt; &lt;input name="message_fish" type="text" value="Enter special order instructions here..." /&gt; &lt;/div&gt;&lt;!-- ITEM_CENTER --&gt; &lt;br /&gt; &lt;div class ="item_right"&gt; Hamburger w/ Fries - $7.00&lt;input name="item[]" type="hidden" value="hamburger"/&gt; &lt;input name="pretty-name_hamburger" type="hidden" value="Hamburger"/&gt;&lt;br&gt; How Many? &lt;input name="quantity_hamburger" type="text" /&gt;&lt;br&gt; &lt;input name="message_hamburger" type="text" value="Enter special order instructions here..." /&gt; &lt;/div&gt;&lt;!-- ITEM_RIGHT --&gt; &lt;br /&gt;&lt;br /&gt; &lt;input type="submit" name="submit" value="Order" /&gt; &lt;/form&gt; </code></pre> <p>OUTPUT: (Similar to)</p> <pre><code>Their Order: --22x Mexican Torta Extra: Enter special order instructions here... --1x Fish Sandwhich Extra: Lots of stuff --2x Hamburger Extra: Mmmm Fries </code></pre> <p>Jared is right. You need to have a way to distinguish between the different items. Hence why I added a extra hidden input in there.</p> <p>Long story short, Every item gets put into an array. PHP goes through this array and picks out what has a quantity value greater than 0. If it has, add that items info into a second array. The last half of this, goes through that new array, and makes it look all nice and pretty for you.</p> <p>If you need me to clarify or fix anything, lemme know.</p> <p>EDIT: Went ahead and added it into the mail function (boredom).</p> <p>PS: I noticed you were using unsanitized varables. I probably shouldn't need to, but I will say it anyways. Make sure you are checking and cleaning user input before you do much with it. Probably not that big of a deal here, but it could still turn bad in certain situations.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload