user541597
user541597

Reputation: 4345

Disabled Select drop down checking through PHP

I have a couple of drop down boxes I access through POST with PHP. When the boxes are disabled and not equal to zero I have the program do something. However when they are disabled I do not want them to do anything. The way I check if the boxes are active is by using an if statement

if ($_POST['box'] != "blank"){

//do something

}

So basically I check if the box is not in the default blank position run the if statement.

However when it is disabled I am not sure how to check or what kind of value it returns if any. What can I add to the if statement so it will not go into the loop when the boxes are disabled?

I tried:

   if ($_POST['box'] != "blank" || $_POST['box'] != ""){

    //do something

    }

But that did not work. Any ideas?

Upvotes: 0

Views: 1373

Answers (2)

mifrai
mifrai

Reputation: 103

When HTML elements are disabled, they do not create an entry within the $_POST variable.

So if you're positive that the POST submission coming in contains a the 'box' field, then you can go:

if (!isset($_POST['box']) || $_POST['box'] != "blank") {
  echo "The box field is disabled or blank";
}

Now, your code wasn't working because $_POST['box'] != "" checks if $_POST['box'] is not an empty string. Having any content will then make your if conditional true.

Upvotes: 1

mfonda
mfonda

Reputation: 8003

It is important to understand that a form is just something that sends an HTTP POST request to a web server. From PHP's point of view, it has no idea that someone clicked submit on a form, it just sees some POST data that could have came from anywhere.

That said, a disabled form element will not be submitted. Instead, try something like

if (!isset($_POST['box'])) {
    //box was not submitted
}

Upvotes: 1

Related Questions