kat35601 Posted June 28, 2018 Share Posted June 28, 2018 My if(empty())Statement is not working what did I do wrong??? <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <style> </style> </head> <body> <?php if(isset($_POST['submit'])){ if(empty($_POST["custid"] || $_POST["item"] || $_POST["qty"])){ echo "Missing Information required"; } } ?> <form action="backorder.php" methd="post"> CustId:<input type="text" name="custid"><br /> Item #:<input type="text" name="item"><br /> Qty :<input type="text" name="qty"><br /> <input type="submit" name="submit" value ="OK"> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
chrisrulez001 Posted June 28, 2018 Share Posted June 28, 2018 7 minutes ago, kat35601 said: My if(empty())Statement is not working what did I do wrong??? <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <style> </style> </head> <body> <?php if(isset($_POST['submit'])){ if(empty($_POST["custid"] || $_POST["item"] || $_POST["qty"])){ echo "Missing Information required"; } } ?> <form action="backorder.php" methd="post"> CustId:<input type="text" name="custid"><br /> Item #:<input type="text" name="item"><br /> Qty :<input type="text" name="qty"><br /> <input type="submit" name="submit" value ="OK"> </form> </body> </html> Hi This line: if(empty($_POST["custid"] || $_POST["item"] || $_POST["qty"])){ echo "Missing Information required"; } } Should be like this: if(empty($_POST["custid"]) || empty($_POST["item"]) || empty($_POST["qty"])){ echo "Missing Information required"; } } Quote Link to comment Share on other sites More sharing options...
Barand Posted June 28, 2018 Share Posted June 28, 2018 $_POST["custid"] || $_POST["item"] || $_POST["qty"] That will return a boolean value. you need if(empty($_POST["custid"] || empty($_POST["item"]) || empty($_POST["qty"])) { But be careful of what exactly is empty, such as 0. Quote Link to comment Share on other sites More sharing options...
kat35601 Posted June 28, 2018 Author Share Posted June 28, 2018 I still do not get the echo "Missing Information required"; <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <style> </style> </head> <body> <?php if(isset($_POST['submit'])){ if(empty($_POST["custid"]) || empty($_POST["item"]) || empty($_POST["qty"])){ echo "Missing Information required"; } } ?> <form action="backorder.php" methd="post"> CustId:<input type="text" name="custid"><br /> Item #:<input type="text" name="item"><br /> Qty :<input type="text" name="qty"><br /> <input type="submit" name="submit" value ="OK"> </form> </body> </html Quote Link to comment Share on other sites More sharing options...
chrisrulez001 Posted June 28, 2018 Share Posted June 28, 2018 I've just noticed on the form it says methd="post". Instead of method="post" But the problem could be because the form is posting to the file called: backorder.php As the form is posting to this file, the error checking would be better suited in there. Although if you wanted to handle the form processing in one file, you could remove the action="backorder.php". 1 Quote Link to comment Share on other sites More sharing options...
kat35601 Posted June 28, 2018 Author Share Posted June 28, 2018 Thanks I looked and looked bet did not see methd="post". Instead of method="post" but that fixed my problem. Thanks Again Quote Link to comment Share on other sites More sharing options...
chrisrulez001 Posted June 28, 2018 Share Posted June 28, 2018 No problem ? Quote Link to comment Share on other sites More sharing options...
benanamen Posted June 28, 2018 Share Posted June 28, 2018 (edited) This if(isset($_POST['submit']) Should be if($_SERVER['REQUEST_METHOD'] == 'POST') Also, you are using HTML4 tags in a HTML5 document Edited June 28, 2018 by benanamen Quote Link to comment Share on other sites More sharing options...
shidhu40 Posted June 29, 2018 Share Posted June 29, 2018 First of all check the $_POST response. i.e Value inside $_POST["custid"] or $_POST["item"] or $_POST["qty"]. Check it value because empty() cannot be used with string variables . So use var_dump(empty($_POST["custid"])) to check its behaviour For example $var1=0; if (empty($var1)) { echo '$var1'." is empty or 0. <br />"; } It will always enter in if statement Then implement validation if((empty($_POST["custid"]) && strlen($_POST["custid"]) == 0) || (empty($_POST["item"]) && strlen($_POST["item"]) == 0) || (empty($_POST["qty"]) && strlen($_POST["qty"]) == 0)){ echo "Missing Information required"; } Quote Link to comment Share on other sites More sharing options...
benanamen Posted June 29, 2018 Share Posted June 29, 2018 Yeah, no @shidhu40. You seem to not understand empty. Checking strlen is redundant and completely unnecessary. Feel free to read the manual on what is considered empty. http://php.net/manual/en/function.empty.php Quote Link to comment Share on other sites More sharing options...
ajoo Posted July 6, 2018 Share Posted July 6, 2018 On 6/29/2018 at 12:43 AM, benanamen said: This if(isset($_POST['submit']) Should be if($_SERVER['REQUEST_METHOD'] == 'POST') Also, you are using HTML4 tags in a HTML5 document Hi Benanaman, is this a big issue ? Could you please explain the difference & the advantage of one over the other? Thanks. Quote Link to comment Share on other sites More sharing options...
benanamen Posted July 6, 2018 Share Posted July 6, 2018 @ajoo, The problem with the first code is that your script is depending on the name of a button to be submitted in order to work. This method will completely fail in certain cases. The second code will never fail. Quote Link to comment Share on other sites More sharing options...
piano0011 Posted July 7, 2018 Share Posted July 7, 2018 I am also wondering here... do you need to put post in capital? Quote Link to comment Share on other sites More sharing options...
benanamen Posted July 7, 2018 Share Posted July 7, 2018 55 minutes ago, piano0011 said: I am also wondering here... do you need to put post in capital? Form method, lowercase , Global Server variables UPPERCASE Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.