Mutley Posted April 23, 2007 Share Posted April 23, 2007 I have a product list page, users can check the product then go to an e-mail form, that lists the products they checked. On this, they can then chose a quantity for every product, how do I make it so when they send the e-mail, it gets the quantity for each product (using PHP mail, to send)? The field would be: <input type="text" size="4" maxlength="4" value="1" name="quantity"> But that wouldn't work for multiple ones? Would I use a "foreach" statement, somehow? Quote Link to comment https://forums.phpfreaks.com/topic/48297-form-posting-multiple-fields/ Share on other sites More sharing options...
per1os Posted April 23, 2007 Share Posted April 23, 2007 <input type="text" size="4" maxlength="4" value="1" name="quantity[]"> That would make it into an array that you can loop through. Quote Link to comment https://forums.phpfreaks.com/topic/48297-form-posting-multiple-fields/#findComment-236104 Share on other sites More sharing options...
MadTechie Posted April 23, 2007 Share Posted April 23, 2007 with forms if your using the same field more than once use this <input type="text" size="4" maxlength="4" value="1" name="quantity[]"> then <?php foreach ($_POST['quantity'] as $q) { echo "$q<br>"; } ?> hope that helps EDIT: Ahhh Frost!!! beat me Quote Link to comment https://forums.phpfreaks.com/topic/48297-form-posting-multiple-fields/#findComment-236106 Share on other sites More sharing options...
Mutley Posted April 23, 2007 Author Share Posted April 23, 2007 How do I do that using mail though? <?php if(isset($_POST['submitemail'])) { $name = $_POST['name']; $telephone = $_POST['telephone']; $address = $_POST['address']; $postcode = $_POST['postcode']; $email = $_POST['email']; $notes = $_POST['notes']; $send_email = "me@googlemail.com"; $send_subject = "Site E-Mail Order"; $send_message = " E-Mail order/enquiry from website: Name: $name Telephone: $telephone E-Mail: $email -------------------- Address: $address $postcode -------------------- Order notes: $notes -------------------- "; $send_header = "From: test@test.com\n"; mail($send_email, $send_subject, $send_message, $send_header); ?> That's how I'm doing it, I can't insert PHP code in there, can I? Quote Link to comment https://forums.phpfreaks.com/topic/48297-form-posting-multiple-fields/#findComment-236134 Share on other sites More sharing options...
Mutley Posted April 23, 2007 Author Share Posted April 23, 2007 I would like them to display like this: Product | Quantity Product2 | Quantity Product3 | Quantity etc, too. Which the "foreach" doesn't do? Quote Link to comment https://forums.phpfreaks.com/topic/48297-form-posting-multiple-fields/#findComment-236161 Share on other sites More sharing options...
MadTechie Posted April 23, 2007 Share Posted April 23, 2007 why won't this work ? <?php foreach ($_POST['quantity'] as $k => $q) { echo "{$_POST['quantity'][$k]} | $q<br>"; } ?> <input type="text" size="10" maxlength="10" value="mice" name="product[]"> <input type="text" size="4" maxlength="4" value="1" name="quantity[]"> Quote Link to comment https://forums.phpfreaks.com/topic/48297-form-posting-multiple-fields/#findComment-236169 Share on other sites More sharing options...
Mutley Posted April 23, 2007 Author Share Posted April 23, 2007 if(isset($_POST['submitemail'])) { $name = $_POST['name']; $telephone = $_POST['telephone']; $address = $_POST['address']; $postcode = $_POST['postcode']; $email = $_POST['email']; $notes = $_POST['notes']; if($name == NULL || $telephone == NULL || $address == NULL || $postcode == NULL || $email == NULL) { include("includes/header.php"); echo "Please fill in <b>all</b> the fields! <a href=\"javascript:history.go(-1)\">Click here to go back</a>."; include("includes/footer.php"); } else { $send_email = "me@googlemail.com"; $send_subject = "Site E-Mail Order"; foreach ($_POST['quantity'] as $k => $q) { $send_message = " E-Mail order/enquiry from website: Name: $name Telephone: $telephone E-Mail: $email -------------------- Address: $address $postcode -------------------- Order notes: $notes -------------------- {$_POST['quantity'][$k]} | $q<br> "; } $send_header = "From: test@test.com\n"; mail($send_email, $send_subject, $send_message, $send_header); ?> <script type="text/javascript"> window.location = "index.php?msg=Your e-mail product enquiry has been sent" </script> <? } HTML isn't allowed in the e-mail, so <br> won't work, is above right though, because when I try it I just get the last quantity not all of them? Quote Link to comment https://forums.phpfreaks.com/topic/48297-form-posting-multiple-fields/#findComment-236180 Share on other sites More sharing options...
MadTechie Posted April 23, 2007 Share Posted April 23, 2007 please post your form code Quote Link to comment https://forums.phpfreaks.com/topic/48297-form-posting-multiple-fields/#findComment-236184 Share on other sites More sharing options...
Mutley Posted April 23, 2007 Author Share Posted April 23, 2007 <form enctype="multipart/form-data" action="mailorder.php" method="post" /> <table class="mail" width="400" border="0" cellspacing="0" cellpadding="0"> <tr> <td>Name:</td> <td><input type="text" size="20" maxlength="35" name="name"></td> </tr> <tr> <td>Telephone:</td> <td><input type="text" size="15" maxlength="15" name="telephone"></td> </tr> <tr> <td>Address:</td> <td><textarea name="address" rows="3" cols="40"></textarea></td> </tr> <tr> <td>Postcode:</td> <td><input type="text" size="7" maxlength="7" name="postcode"></td> </tr> <tr> <td>E-Mail:</td> <td><input type="text" size="20" maxlength="35" name="email"></td> </tr> <tr> <td>Order Notes:</td> <td><textarea name="notes" rows="3" cols="40"></textarea></td> </tr> </table> <br /><br /> Your selected items: <br /><br /> <table class="mail" width="600" border="0" cellspacing="0" cellpadding="0"> <td>JJM #:</td> <td>Product #:</td> <td>Category:</td> <td>Description:</td> <td>Weight*:</td> <? if(!empty($_COOKIE['id']) && !empty($_COOKIE['pass'])) { $userid = htmlspecialchars($_COOKIE['id'], ENT_QUOTES); $pass = htmlspecialchars($_COOKIE['pass'], ENT_QUOTES); $check = mysql_query("SELECT * FROM users WHERE password = '$pass' AND id = '$userid'") or die(mysql_error()); if(mysql_num_rows($check) <> 1) { echo ""; } else { /////////////// ### PRICE ### ?> <td>Price*:</td> <? ////////////////////// End login ### PRICE ### } } else { echo ""; } ?> <td>Quantity:</td> <? foreach ($_POST['order'] as $id => $x) { $sql = "SELECT id, category, name, cpn, pn, description, descriptionf, descriptiong, weight, price, ofeatured, onew, osale FROM products WHERE id = ".$id." LIMIT 1"; $result = mysql_query($sql) or die("SQL Query: {wordwrap($sql)}<br />Error because: " . mysql_error()); if(mysql_num_rows($result)!=0) { while(list($id, $category, $name, $cpn, $pn, $description, $descriptionf, $descriptiong, $weight, $price, $ofeatured, $onew, $osale) = mysql_fetch_row($result)) { ?> <!-- START ORDER FORM --> <tr> <td><?=$cpn?> </td> <td><?=$pn?> </td> <td><?=$category?> </td> <td><?=$description?> </td> <td> <?=$weight?> </td> <? if(!empty($_COOKIE['id']) && !empty($_COOKIE['pass'])) { $userid = htmlspecialchars($_COOKIE['id'], ENT_QUOTES); $pass = htmlspecialchars($_COOKIE['pass'], ENT_QUOTES); $check = mysql_query("SELECT * FROM users WHERE password = '$pass' AND id = '$userid'") or die(mysql_error()); if(mysql_num_rows($check) <> 1) { echo ""; } else { /////////////// ### PRICE ### ?> <td>€<?=$price?> </td> <? ////////////////////// End login ### PRICE ### } } else { echo ""; } ?> <td> <input type="hidden" name="price[]" value="<?=$price?>" /> <input type="hidden" name="cpn[]" value="<?=$cpn?>" /> <input type="hidden" name="pn[]" value="<?=$pn?>" /> <input type="text" size="4" maxlength="4" value="1" name="quantity[]"> </td> </tr> <!-- END ORDER FORM --> <? } } // Split closing between results and selected } ?> </table> * <i>Per unit.</i><br /><br /> <input type="submit" name="submitemail" value="Submit E-Mail" /> Thanks, I have other hidden fields in there, when I know how to get the quantity working I'll do the same to the $cpn, $pn you see in the hidden fields. Quote Link to comment https://forums.phpfreaks.com/topic/48297-form-posting-multiple-fields/#findComment-236189 Share on other sites More sharing options...
MadTechie Posted April 23, 2007 Share Posted April 23, 2007 i have created my own script which does basically what i think your after if you paste it into its own file and test it you might be able to use parts, as a note i used an array instead of a SQL statement <?php if( isset($_POST['submit']) ) { $body = ""; foreach($_POST['Product'] as $k => $p) { $body .= "$p | {$_POST['quantity'][$k]}\n"; } echo nl2br($body); // of course don't use nl2br on the mail }else{ ?> <form method="post"> <?php $item = array("fish", "chips", "Beans", "i missed lunch"); $q = array("1", "65", "1002", "lol"); foreach($item as $k => $p) { ?> <input type="text" value="<?php echo $p ?>" name="Product[]"><br /> <input type="text" size="4" maxlength="4" value="<?php echo $q[$k] ?>" name="quantity[]"><br /> <br /> <?php } ?> <input name="submit" type="submit" value="SendMail" /> </form> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/48297-form-posting-multiple-fields/#findComment-236200 Share on other sites More sharing options...
Mutley Posted April 23, 2007 Author Share Posted April 23, 2007 Thanks a lot, just trying to understand it, what does the => part mean? Quote Link to comment https://forums.phpfreaks.com/topic/48297-form-posting-multiple-fields/#findComment-236217 Share on other sites More sharing options...
MadTechie Posted April 23, 2007 Share Posted April 23, 2007 <?php foreach($_POST['Product'] as $k => $p) ?> $_POST['Product'] = array $k = Key $p = value is if $_POST['Product'] = array(0 => "123", 1 => "234", 2 => "345") then on the first loop $k = 0 $p = "123" 2nd loop $k=1 $p= "234" etc Quote Link to comment https://forums.phpfreaks.com/topic/48297-form-posting-multiple-fields/#findComment-236228 Share on other sites More sharing options...
Mutley Posted April 23, 2007 Author Share Posted April 23, 2007 So how do you join the quantity and product fields? If I wanted it like this: foreach($_POST['quantity'] as $k => $p) { $body .= "$cpn | $pn | $price | {$_POST['quantity'][$k]}\n"; } echo $body; The part I can't do is the foreach on the form as that data is already getting looped by a SQL query. Quote Link to comment https://forums.phpfreaks.com/topic/48297-form-posting-multiple-fields/#findComment-236242 Share on other sites More sharing options...
Mutley Posted April 23, 2007 Author Share Posted April 23, 2007 I tried: foreach($_POST['cpn'] as $cpn) { foreach($_POST['pn'] as $pn) { foreach($_POST['price'] as $price) { foreach($_POST['quantity'] as $quantity => $cpn) { $body .= "$cpn | $pn | $price | {$_POST['quantity'][$cpn]}\n"; } } } } But it didn't work, just got spammed with loads of "$cpn" field. Quote Link to comment https://forums.phpfreaks.com/topic/48297-form-posting-multiple-fields/#findComment-236250 Share on other sites More sharing options...
MadTechie Posted April 23, 2007 Share Posted April 23, 2007 {$_POST[$k]['quantity']} Quote Link to comment https://forums.phpfreaks.com/topic/48297-form-posting-multiple-fields/#findComment-236257 Share on other sites More sharing options...
Mutley Posted April 23, 2007 Author Share Posted April 23, 2007 This is really confusing me, lol. So is that what joins things? Can you help with my example above what I tried, it's all very new to me the foreach statement. Quote Link to comment https://forums.phpfreaks.com/topic/48297-form-posting-multiple-fields/#findComment-236290 Share on other sites More sharing options...
Mutley Posted April 24, 2007 Author Share Posted April 24, 2007 Still having problems with this, like above. ??? Quote Link to comment https://forums.phpfreaks.com/topic/48297-form-posting-multiple-fields/#findComment-237399 Share on other sites More sharing options...
Mutley Posted May 5, 2007 Author Share Posted May 5, 2007 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/48297-form-posting-multiple-fields/#findComment-246133 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.