Jump to content

Saving selected products


mclamais

Recommended Posts

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.

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.