ckehrer Posted August 2, 2009 Share Posted August 2, 2009 Thanks in advance, I'm currently working on a shopping cart where I have a quantity textbox field with a default of 0 when the page loads. I want to be able to override the default value when a user enters a number inside that field, and have that value passed in a query string to the checkout page. Quote Link to comment https://forums.phpfreaks.com/topic/168509-quantity-textbox/ Share on other sites More sharing options...
Bricktop Posted August 2, 2009 Share Posted August 2, 2009 Hi ckehrer, You need to give the textbox a "name=" attribute and then turn the $_POSTed value from that field into a variable and then pass to the database or whatever. For example: <input name="quantity" value="0" /> <?php $quantity = $_POST[quantity]; ?> Hope that makes sense! Quote Link to comment https://forums.phpfreaks.com/topic/168509-quantity-textbox/#findComment-888910 Share on other sites More sharing options...
ckehrer Posted August 2, 2009 Author Share Posted August 2, 2009 Hi thanks for the quick reply. Here is a little bit of my code. <?php $quantity = 0; ?> <td><input type='text' size='2' name='quantity' value='<? echo $quantity; ?>' /></td> I'm setting a default variable to zero then would like to pass via query string using $_GET i.e if a user enters 5 then I want to override the default with 5. Is this possible? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/168509-quantity-textbox/#findComment-888920 Share on other sites More sharing options...
gevans Posted August 2, 2009 Share Posted August 2, 2009 <?php $quantity = isset($_GET['quantity']) ? (int) $_GET['quantity'] : 0; Quote Link to comment https://forums.phpfreaks.com/topic/168509-quantity-textbox/#findComment-888927 Share on other sites More sharing options...
ldougherty Posted August 2, 2009 Share Posted August 2, 2009 this should already be configured, how is your form data being sent via GET or POST? On the line that says <FORM METHOD=??> what is the method? If it is POST then you should retrieve the value as $_POST['quantity'] or if it is GET then you should retrieve the value as $_GET['quantity'] Quote Link to comment https://forums.phpfreaks.com/topic/168509-quantity-textbox/#findComment-888928 Share on other sites More sharing options...
ckehrer Posted August 2, 2009 Author Share Posted August 2, 2009 I'm using $_GET for my form. I think the problem might be in my loop here is a sample of my code. I'm dynamically populating a products page with all the products and prices from an array strProducts. <? $i = 0; while(list($key,$value) = each($strProducts)) { // Populate page with array of products and prices $i++; $quantity = isset($_GET['quantity']) ? (int) $_GET['quantity'] : 0; ?> <tr> <td><input type='hidden' name='product' value='<? echo $key; ?>' /><? echo $key; ?></td> <td><input type='hidden' name='price' value='<? echo $value; ?>' /><? echo $value; ?></td> <td><input type='text' size='2' name='quantity' value='<? echo $quantity; ?>' /></td> <input type='hidden' name='itemID' value='<? echo $i; ?>'> <td><a style='text-decoration: none;' id="add" style='margin-right: 15px;'; href='/checkout.php?product=<? echo $key; ?>&price=<? echo $value; ?>&itemID=<? echo $i; ?>&quantity=<? echo $quantity; ?>'><input type='submit' name='btnSubmit' value='Add to cart' /></a></td> <input type='hidden' name='<? echo session_id(); ?>' /> <? } ?> Quote Link to comment https://forums.phpfreaks.com/topic/168509-quantity-textbox/#findComment-888932 Share on other sites More sharing options...
gevans Posted August 2, 2009 Share Posted August 2, 2009 Are you displaying severall quantity input fields? If noty why are you using a loop? Quote Link to comment https://forums.phpfreaks.com/topic/168509-quantity-textbox/#findComment-888936 Share on other sites More sharing options...
ckehrer Posted August 2, 2009 Author Share Posted August 2, 2009 Yes I have a quantity textbox next to each individual product. Quote Link to comment https://forums.phpfreaks.com/topic/168509-quantity-textbox/#findComment-888942 Share on other sites More sharing options...
gevans Posted August 2, 2009 Share Posted August 2, 2009 In which case you need to add the name attribute of quantity as an array name="quantity[]" Quote Link to comment https://forums.phpfreaks.com/topic/168509-quantity-textbox/#findComment-889049 Share on other sites More sharing options...
ckehrer Posted August 2, 2009 Author Share Posted August 2, 2009 Thanks everybody for all the help. I'm still only able to pass the default value but I'll keep working on it. Quote Link to comment https://forums.phpfreaks.com/topic/168509-quantity-textbox/#findComment-889133 Share on other sites More sharing options...
ckehrer Posted August 3, 2009 Author Share Posted August 3, 2009 Please help. I've tried a combination of of altering the query string, but I still keep passing the default value of 0 even if I enter a value in the text box field. I'm using $quantity = $_GET['quantity']; on the checkout page to receive the values passed. Again thanks everyone for your help. <?php $i = 0; while(list($key,$value) = each($strProducts)) { // Populate page with array of products and prices $i++; $quantity = isset($_GET['quantity']) ? (int) $_GET['quantity'] : 0; ?> <tr> <td><input type='hidden' name='product' value='<? echo $key; ?>' /><? echo $key; ?></td> <td><input type='hidden' name='price' value='<? echo $value; ?>' /><? echo $value; ?></td> <td><input type='text' size='2' name='quantity[]' value='<? echo $quantity; ?>' /></td> <input type='hidden' name='itemID' value='<? echo $i; ?>'> <td><a style='text-decoration: none;' id="add" style='margin-right: 15px;'; href='/checkout.php?product=<? echo $key; ?>&price=<? echo $value; ?>&itemID=<? echo $i; ?>&quantity=<? echo $quantity; ?>'><input type='submit' name='btnSubmit' value='Add to cart' /></a></td> <input type='hidden' name='<? echo session_id(); ?>' /> <? } ?> Quote Link to comment https://forums.phpfreaks.com/topic/168509-quantity-textbox/#findComment-889603 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.