Gary Lee
Gary Lee

Reputation: 23

Setting multiple if conditions in php

I am currently making a self-processing form to display products that allows users to enter a quantity. I will then save the quantity in

$_POST['qty_entered']

when the users press the submit button. However, I also have an if statement to ensure that the users entered a valid quantity

    if (is_numeric($_POST['qty_entered'][0]) && is_int($_POST['qty_entered'][0]) && $_POST['qty_entered'][0] > 0) {
    print "'<tr><td>' test '</td>'";
}
else {
    $errors = true}

Then want to print a table with the invoice on the same page, only after the submit button is pressed and if the user entered a valid quantity. However, this code is not working:

if (array_key_exists ('submit_button', $_POST)) && $errors = false{
print "Invoice";

I also have a code at the beginning of the form to set $errors to false, and if $errors is true, it will print an error message. However, it is also not working because it doesn't display the error message when I type rubbish that would trigger the if statement such as "agfasgs" or "-1"

        $errors = false;
    if ($errors == true) {
print "Please enter postive whole numbers for quantity <br><br>";}

Thank you for the help!

Upvotes: 1

Views: 43

Answers (2)

Rotimi
Rotimi

Reputation: 4825

This line does not have the brackets properly inserted. Also you need == or ===(This enforces strict check) What you have currently assigns the variable. You need to check.

The === operator is supposed to compare exact content equality while the == operator would compare semantic equality

if (array_key_exists ('submit_button', $_POST)) && $errors = false{//This should have thrown an error though(FATAL)

It should be

if (array_key_exists ('submit_button', $_POST) && $errors == false){

Also, FYI, shouldnt this line

if (is_numeric($_POST['qty_entered'][0]) && is_int($_POST['qty_entered'][0]) && $_POST['qty_entered'][0] > 0) {

Be this: ?

if (is_numeric($_POST['qty_entered']) && is_int($_POST['qty_entered']) && $_POST['qty_entered'] > 0) {//access the post data properly

Upvotes: 1

Curtis Mattoon
Curtis Mattoon

Reputation: 4742

php if (array_key_exists ('submit_button', $_POST)) && $errors = false{ print "Invoice";

is not valid PHP: Your parens are wrong, and you're assigning false to $errors (you want ==, not =)

if (array_key_exists('submit_button', $_POST) && $errors == false) {

Upvotes: 0

Related Questions