Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/168509-quantity-textbox/
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/168509-quantity-textbox/#findComment-888910
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/168509-quantity-textbox/#findComment-888920
Share on other sites

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']

Link to comment
https://forums.phpfreaks.com/topic/168509-quantity-textbox/#findComment-888928
Share on other sites

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(); ?>' />	<?				 

}		
?>	

Link to comment
https://forums.phpfreaks.com/topic/168509-quantity-textbox/#findComment-888932
Share on other sites

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(); ?>' />	<?				 

}		
?>			

Link to comment
https://forums.phpfreaks.com/topic/168509-quantity-textbox/#findComment-889603
Share on other sites

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.