Jump to content

if (empty()) not working for me


kat35601

Recommended Posts

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>

 

Link to comment
Share on other sites

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";
}
}

 

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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".

Link to comment
Share on other sites

 

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";
}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.