mclamais Posted June 27, 2009 Share Posted June 27, 2009 OK, I have a quantity field as follows <input id="quantity" name="product[<?=$row1['sku'];?>]" type="text" size="2" /> Results in HTML like: <input id="product" name="product[ABC-8564]" type="text" size="2" /> <input id="quantity" name="product[ABC-8565]" type="text" size="2" /> The customer selects the desired products by adding a quantity value to the product(s) they're interested in, and clicks update. The form self posts, and returns with the same list of products. I need help with saving the products selected after the form is refreshed and leaving the quantity field populated with the users desired quantity in the field. So I would like the user selected products field to look like this after the form posts, unselected products would remain the same. <input id="product" name="product[ABC-8564]" type="text" size="2" /> <input id="quantity" name="product[ABC-8565]" type="text" size="2" value="2" /> Thanks for your time. Quote Link to comment https://forums.phpfreaks.com/topic/163899-saving-selected-products/ Share on other sites More sharing options...
HPWebSolutions Posted June 27, 2009 Share Posted June 27, 2009 First, is it correct that your second input field has an id of product and your second has an id of quantity? Are they not both quantity fields? If I'm understanding your question correctly, all you would need to do is output the value of $_POST[product[PRODUCT_NUM]] in the value attribute for quantity. I.E.: <input id="quantity" name="product[ABC-8565]" type="text" size="2" value="<?php echo $_POST['product['.$row1['sku'].']']; ?>" /> Remember to properly escape the post data before outputting it to the client. Quote Link to comment https://forums.phpfreaks.com/topic/163899-saving-selected-products/#findComment-864728 Share on other sites More sharing options...
mclamais Posted June 27, 2009 Author Share Posted June 27, 2009 First, is it correct that your second input field has an id of product and your second has an id of quantity? Are they not both quantity fields? Yes, sorry they are both quantity fields. Thanks for the code, I'll give that a try. Quote Link to comment https://forums.phpfreaks.com/topic/163899-saving-selected-products/#findComment-864790 Share on other sites More sharing options...
mclamais Posted June 27, 2009 Author Share Posted June 27, 2009 OK I added this: value="<?php echo $_POST['product['.$row1['sku'].']']; ?>" and after posting the form I get: <input id="product" name="product[bGS-110003-400]" type="text" size="2" maxlength="2" value="<br /> <b>Notice</b>: Undefined index: product[bGS-110003-400] in... and it repeat for each field returned. Quote Link to comment https://forums.phpfreaks.com/topic/163899-saving-selected-products/#findComment-864825 Share on other sites More sharing options...
HPWebSolutions Posted June 29, 2009 Share Posted June 29, 2009 That is very weird. Can you send me the code that displays the form? Quote Link to comment https://forums.phpfreaks.com/topic/163899-saving-selected-products/#findComment-865359 Share on other sites More sharing options...
mclamais Posted July 1, 2009 Author Share Posted July 1, 2009 Again, thanks for the assistance. Here is the PHP for one quantity field: <div class="detail-quantity">Quantity: <input id="product" name="product[<?=$row1['sku'];?>]" type="text" size="2" maxlength="2" <?php if(isset($_POST['updateform'])){ $val = $_POST['product['.$row1['sku'].']']; echo "value='".$val."'"; } ?> onChange="if(!validate(this.value)) alert('Please enter numbers only')" /> </div> Here is the results of one quantity field on page load (before form submission): <div class="detail-quantity">Quantity: <input id="product" name="product[bGS-110003-400]" type="text" size="2" maxlength="2" onChange="if(!validate(this.value)) alert('Please enter numbers only')" /> </div> Here is the results of the same quantity field after form submission: <div class="detail-quantity">Quantity: <input id="product" name="product[bGS-110003-400]" type="text" size="2" maxlength="2" <br /> <b>Notice</b>: Undefined index: product[bGS-110003-400] in <b>/Users/Marc/Sites/web_bgscabinets/wiz2/platinum/step2_grid_a.php</b> on line <b>36</b><br /> value='' onChange="if(!validate(this.value)) alert('Please enter numbers only')" /> </div> Here is the main form PHP: <form id="form1" name="form1" method="post" action="step2.php#cart"> <input type="hidden" name="p" value="<?=$p?>"> <input type="hidden" name="type" value="<?=$type?>"> <input type="hidden" name="paint" value="<?=$paint_id?>"> <input type="hidden" name="updateform" value="1"> <?php // Results 1 --------------------------- $query1 = "SELECT * FROM products WHERE prod_type_id = " .$type_id_a. " AND paint_id = " .$paint_id. " ORDER BY price ASC"; //echo $query1; $result1 = mysql_query($query1) or die(mysql_error()); $rcount1 = mysql_num_rows($result1); if($rcount1>0){ ?> <div id="step2-div-a"> <?php require_once('step2_grid_a.php'); ?> </div> <?php } ?> </form> The include step2_grid_a.php: <table border="0" width="100%" cellpadding="0" cellspacing="0"> <tr> <?php $numcols = 3; // how many columns to display $numcolsprinted = 0; // no of columns so far while($row1 = mysql_fetch_assoc($result1)){ if ($numcolsprinted == $numcols) { print "</tr>\n<tr>\n"; $numcolsprinted = 0; } echo "<td width='33%' class='step2'>\n"; ?> <table border="0" width="" cellpadding="0" cellspacing="0"> <tr> <td colspan="2"><a name="<?=$row1['product_name'];?>"</a><div class="product-name"><?=$row1["product_name"];?></div></td> </tr> <tr> <!-- col1--> <td valign="top" style="width:100px;"><a href="#" onclick="window.open('../detail.php?prod_id=<?=$row1["prod_id"];?>','Details','scrollbars=1,menubar=0,resizable=1,width=800,height=700');"> <img src="../images/<?=$row1["image_cart"];?>" width="100" height="100" alt="<?=$row1["product_name"];?>" border="0" /></a></td> <!-- col2--> <td valign="top" style="padding-left:10px;"> <div class="detail-model-number"><?php echo "Model: ".$row1["model_no"];?></div> <div class="detail-price"><?php echo "$".number_format($row1["price"],2);?></div> <div class="detail-shipping">Shipping: <?php echo "$".number_format($row1["shipping"],2);?></div> <div class="detail-quantity">Quantity: <input id="product" name="product[<?=$row1['sku'];?>]" type="text" size="2" maxlength="2" <?php if(isset($_POST['updateform'])){ $val = $_POST['product['.$row1['sku'].']']; echo "value='".$val."'"; } ?> onChange="if(!validate(this.value)) alert('Please enter numbers only')" /> </div> </td> </tr> </table> <?php echo "</td>\n"; // bump up row counter $numcolsprinted++; } // end while loop $colstobalance = $numcols - $numcolsprinted; //for ($i=1; $i<=$colstobalance; $i++) { //} //print "<td></td>\n"; ?> </tr> </table> <?php mysql_free_result($result1); ?> Quote Link to comment https://forums.phpfreaks.com/topic/163899-saving-selected-products/#findComment-867370 Share on other sites More sharing options...
HPWebSolutions Posted July 4, 2009 Share Posted July 4, 2009 The problem is that when the form is submitted you are posting the field with name product[bGS-110003-400]. What PHP sees looks like an array and you end up with a multidimensional array with 'product' as the first key and 'BGS-110003-400' as the second. So what you'll want to do to get that value is use it as a multidimensional array. You would use $_POST['product']['BGS-110003-400'] to access the posted value for this particlar field. Many things in web site design often operate a little differently from what you would expect (case in point: CSS). Make sure you don't use any funny characters in your array names as well. Quote Link to comment https://forums.phpfreaks.com/topic/163899-saving-selected-products/#findComment-868898 Share on other sites More sharing options...
mclamais Posted July 9, 2009 Author Share Posted July 9, 2009 Thanks that worked great I hard coded $_POST['product']['BGS-110003-400'] and it put the quantity in perfect But now I can't seem to format it correctly to get the SKU from the database. I tried this and other variations $val = $_POST['product']['.$row1['sku'].']; syntax error, unexpected T_STRING, expecting ']' $val = "$_POST['product']['".$row1['sku']."']"; unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/163899-saving-selected-products/#findComment-871800 Share on other sites More sharing options...
HPWebSolutions Posted July 18, 2009 Share Posted July 18, 2009 I haven't been on in a few days. Have you gotten this to work yet? If not, please post the current code snippet you are using to get it out of the database. Quote Link to comment https://forums.phpfreaks.com/topic/163899-saving-selected-products/#findComment-877474 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.