If statement for a form

Ok so I have an html form that submits to a php file… which for the most part works. It comes back with everything I need, but with one flaw. This is an order form where you enter in the # of items you want and then it calculates it for you…and when you submit it shows you the contract with everything you ordered. If the amount you entered is not 0 it should show the number and the price with what you ordered…if you didn’t it shouldn’t show anything at all… This works great except that even if there is just a 0 the contract will show 0 0.00 (the # and total) for items anyways…

code is as follows:

<?php
$qtyA=qtyA;
if ($qtyA != “0”) {
echo $_POST[“qtyA”]; }?>

now originally I had it as this but it does the same thing:

<td><?php
    if (qtyA != "0") { 
    echo $_POST["qtyA"]; }?>
</td>

I have tried using >0 and >=1, but it all just spits out the same result. How do I get it to not so the items that are just 0 ??

I’m taking a guess that you’re not understanding how PHP pulls in variables from a form. It’s usually from $_POST, so in your case it would be $_POST[‘qtyA’].

This is how I would do it:

[php]if (isset($_POST[‘qtyA’])) {
$qtyA = $_POST[‘qtyA’];
}[/php]

Or a fancier way of doing it:

[php] if (!isset($_POST[‘qtyA’]) || !filter_var($_POST[‘qtyA’], FILTER_VALIDATE_INT, array(‘min_range’ => 1))) {
throw new Exception(‘It’s either empty or not a number.’);
} else {
$qtyA = $_POST[‘qtyA’]; // We know its a number.
}[/php]

Also check into http://www.php.net//manual/en/function.number-format.php on how to format a number.

I do get that it gets the info via $_POST… I was just hoping that my if statement would eliminate the printing of the 0 values from the form… but strangely enough your way worked like a charm!!!.. I was thinking that since the values of 0 were already being set in a sense that the isset function wouldn’t work… Thank you soooooo much for your help. :slight_smile:

p.s. sorry…I am very new to this… and sadly a lot of the learning materials I am finding aren’t as thorough as I need.

Sponsor our Newsletter | Privacy Policy | Terms of Service