Jump to content
kat35601

if (empty()) not working for me

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>

 

Share this post


Link to post
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";
}
}

 

 

Share this post


Link to post
Share on other sites
$_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.

Share this post


Link to post
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

Share this post


Link to post
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".

  • Thanks 1

Share this post


Link to post
Share on other sites

Thanks I looked and looked bet did not see  methd="post". Instead of method="post" but that fixed my problem.

Thanks Again

 

Share this post


Link to post
Share on other sites

This

if(isset($_POST['submit'])

Should be

 if($_SERVER['REQUEST_METHOD'] == 'POST')

Also, you are using HTML4 tags in a HTML5 document

Edited by benanamen

Share this post


Link to post
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";
}

 

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

Share this post


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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • 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.