开发者

Check the value of a text input

I have a PayP开发者_JAVA技巧al 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?


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();
  }
});


if ($_POST['field_name'] > 0) { ... }


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';
}

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜