views:

45

answers:

4

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

A: 
if ($_POST['field_name'] > 0) { ... }
Kristopher Ives
+2  A: 

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();
  }
});
Jonathan Sampson
Funny that the Javascript snipplet gets top answer on a PHP question.
Kristopher Ives
Kristopher, sure, when the OP is confused about the proper terminology and technology.
Jonathan Sampson
A: 

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.

fsb
This can be done using (int)@$_POST['quantity']
Kristopher Ives
A: 

This may help:

if (empty($var)) {
    echo '$var is either 0, empty, or not set at all';
}

http://php.net/manual/en/function.empty.php

Andy