W3Ask













HOME  >  PROGRAMMING  >  PHP

Transmit the quantity button number to another page



Perhaps you want to transmit the value of the quantity button + - with a products that you are selling on your website but you don’t know a lot about javascript.

Here is a script that prevent to get a negative number :

<script type="text/javascript">
function add( name ) {
document.getElementById( name ).value ++;
}
function substract( name ) {
if ( document.getElementById( name ).value > 0 )
document.getElementById( name ).value --;
}

function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;

return true;
}
</script>

<form>
<input type="text" value="1" id="quantity" size="2" maxlength="4" onkeypress="return isNumberKey(event);">
<a onClick="add(‘quantity’);" >plus</a>
<a onClick="substract(‘quantity’);"> minus</a>
</form>

And now you want to transmit the quantity to your basket, or limit the quantity in order that it won’t be over the stock one available in you database with the $stock variable.

To limit the quantity, you need to add a condition with the function add() :

function add( name ) {
if (document.getElementById( name ).value < <?php echo intval($stock); ?>) {
document.getElementById( name ).value ++;
}
}

You could also use an input with the type number to generate the field quantity, the only issue is that it is not displayed the same way on the different browser.

To send the quantity to your basket, you can use a form, for example :

<form action="basket.php" method="get">
<input type="hidden" name="action" value="add" />
<input type="hidden" name="r" value="<?php echo htmlspecialchars($ref); ?>" />
<input type="hidden" name="l" value="<?php echo htmlspecialchars($wording); ?>" />
<input type="hidden" name="p" value="<?php echo htmlspecialchars($price); ?>" />

<input type="text" value="1" id="quantity" name="yourquantityattribute" size="2" maxlength="4" onkeypress="return isNumberKey(event);">
<a onClick="add(‘quantity’);" >plus</a>
<a onClick="substract(‘quantity’);"> minus</a>

<button type="submit"><img src="images/product-details/caddieshop.png"></button>
</form>

The POST method would be more appropriate for this form to avoid to have urls full of parameters. But the GET method will allow you to verify easily the information during the development. By submitting the form, you should get the URL that will add the value to the basket.

PS : don’t forget to add the name attribute to the input balises, it will be that name that will be used to get the parameter in PHP via =_GET[‘yourquantityattribute’] or $_POST[‘yourquantityattribute’].

Note: Commissions may be earned from the links above.


This page contains references to products from one or more of our advertisers. We may receive compensation when you click on links to those products. For an explanation of our advertising policy, please visit this page.









Related posts:


How to add page numbers to WordPress to navigate
https://w3ask.com/add-page-numbers-wordpress-pages-navigate/
How can we add numbers to the pages of WordPress in order to allow the visitor to navigate across the website? These numbers redirect the user ...
How to add a share button on social networks with WordPress
https://w3ask.com/add-share-button-social-networks-wordpress/
Nowadays, the use of social networks is essential if you want to make your website known. Adding a button to your articles allows users to share ...
Remove blank pages and page numbers with Calibre
https://w3ask.com/remove-blank-pages-page-numbers-calibre/
Calibre is a file converter, dedicated to electonics books. With it, you can adapt a file to be able to read it on an ebook reader. You can ...
How to convert a number in time with Excel ?
https://w3ask.com/convert-number-in-time-excel/
When we enter a date with points, is there a possibility to convert it automatically into a date with slashes, so that it could be taken into ...
Count the number of occurrence in Excel
https://w3ask.com/number-occurrence-excel/
How to count the number of occurrence of values in a column? For example we have a list of towns in the A column. Among this towns, some ...












Languages

  • English
  • French
  • German
  • Italian
  • Dutch
  • Portuguese
  • Spanish

Know us

  • About
  • Contact
  • Categories list

Privacy

  • Privacy & Cookie Policy
  • Advertising policy
W3Ask © 2023. All rights reserved.
Disclaimer:

When you click on links to various merchants on this site and make a purchase, this can result in this site earning a commission. Affiliate programs and affiliations include, but are not limited to, the eBay Partner Network.

As an Amazon Associate I earn from qualifying purchases.

This page includes affiliate links for which the administrator of W3Ask may earn a commission at no extra cost to you should you make a purchase. These links are indicated using the hashtag #ad.


Information:

We do not assume any responsibility for the data displayed on our website. Please use at your own risk. Some or all of this data may be out of date or incomplete, please refer to the technical page on the respective manufacturer's website to find the latest up-to-date information regarding the specifics of a product.


Trading System Forex GadgetVersus GadgetVersus France