I have a PayPal button with a quantity text field. How could I check to ensure this textfeild is > 0 so that it does not add to cart if quantity is not an integer >= 1?
Thanks
I have a PayPal button with a quantity text field. How could I check to ensure this textfeild is > 0 so that it does not add to cart if quantity is not an integer >= 1?
Thanks
Paypal carts are smart enough not to count negative orders. But you could also provide some javascript logic to your client-side that would prevent the action from taking place if the value is less than 1.
A bit of Javascript/jQuery as an example:
$("submit").click(function(e){
var qty = $(this).closest("form").find("[name='qty']").val();
if (qty < 1) {
e.preventDefault();
}
});
For this I normally use:
if ( isset($_POST['quantity'])
&& preg_match('/^[1-9]\d*$/', $_POST['quantity'] ) {
}
the first test ensure you don't trigger an error if the quantity
isn't in the $_POST
array. the second ensures that the string has only digits and the first isn't zero.
This may help:
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}