In this code, it should change the quantity, but its not changing. its always setting the value '1'.
<?php
if(isset($_POST["quantity"]))
$quantity = settype($_POST["quantity"], "integer");
else
$quantity = 1;
$item_price = 5.99;
printf("%d x item = $%.2f",
$quantity, $quantity * $item_price);
?>
<FORM ACTION="buy.php" METHOD=POST>
Update quantity:
<INPUT NAME="quantity" SIZE=2
VALUE="<?php echo $quantity;?>">
<INPUT TYPE=SUBMIT VALUE="Change quantity">
</FORM>
</body>
</html>
settype
is used to set type of variable, it returns bool
value indicating success or failure. You need to assign $_POST
value to $quantity
(with type cast), try this:
if(isset($_POST["quantity"])) {
$quantity = (int)$_POST["quantity"];
} else {
$quantity = 1;
}
// or
$quantity = isset($_POST["quantity"]) ? (int)$_POST["quantity"] : 1;
Try with
if(isset($_POST["quantity"]))
$quantity = intval($_POST["quantity"]);