1

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!

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

I have 5 quantities in the $_POST['qty_entered'] which is why I want it to refer to the first quantity, which is [0]. However the == fixed the fatal error that I was receiving. Thanks! However I still have the problem where it's not setting the $errors = TRUE when an error is found in the quantity
I am still stuck on the last part of the code where it sets $errors = TRUE if the if conditions aren't met.. var_dumping $errors returns false even though I typed in something weird such as -1 or sagsad
try remove the is_int condition
Actually, I have $errors at the beginning of the code so that it does not execute anything and just print an error message if those conditions aren't met.. So var_dumping $errors at the beginning returns false, however var_dumping $errors at the end after the if conditions returns true.. Why isn't it making the one at the beginning true as well since I thought a self-processing form would run itself again and it should change that boolean to true if there is an error
0

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) {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.