Brian Curless
Brian Curless

Reputation: 245

PHP Post Working with Fields

I have a very simple form:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<form method="post" action="">
<label>Filter By Zip Code:
<input maxlength="5" name="zipcode" size="6" type="text" /></label>
<label>Filter By Products:
 <select name="products">
  <option value="">Select Product</option>
  <option value="jewelry">Jewelry</option>
  <option value="homedecor">Home Decor</option>
  <option value="kitchen">Kitchen</option>
</select> </label>
    <label>Filter By Materials:
 <select name="materials">
  <option value="">Select Material</option>
  <option value="abs">ABS</option>
  <option value="gold">Gold</option>
  <option value="silver">Silver</option>
</select> </label>
<br>
<input name="submit" type="submit" value="Submit" />
<input name="clear" type="submit" value="Clear" />
</form>
<?php
if (!isset($_POST['submit'])){
        echo "Nothing Selected";
}
elseif(isset($_POST['zipcode']) && ($_POST['zipcode'] != "")) {
$zip = $_POST['zipcode'];
        echo "Zip data " . $zip;
}

elseif(isset($_POST['products'])  && ($_POST['products'] != "")) {
$products = $_POST['products'];
        echo "Products data " . $products;
}
elseif(isset($_POST['materials'])   && ($_POST['materials'] != "")) {
$materials = $_POST['materials'];
        echo "Materials data " . $materials;
}
?>

When I am posting data It will only take the value of the first item selected.

I have tried breaking up the if ... elseif and doing some really redundant statements.

ie:

elseif(isset($_POST['materials']) && ($_POST['materials'] != "") || ($_POST['products'])  && ($_POST['products'] != "") || $_POST['zipcode'])  && ($_POST['zipcode'] != "")) {

That seems really to complicated and i cannot believe there is not an easier way.

I have tried using a switch ... case however get the same results.

What im looking for is what if two filters are selected?

What if all three are selected?

How can I get the data to post out correctly.

Upvotes: 0

Views: 93

Answers (3)

AntoineB
AntoineB

Reputation: 4694

You're checking if "zipcode" is posted, and if it's not, if "products" is posted, etc...

You should change your "else if"s to "if" :

<?php
if (!isset($_POST['submit']))
{
    echo "Nothing Selected";
}
else
{
    if(isset($_POST['zipcode']) && ($_POST['zipcode'] != ""))
    {
        $zip = $_POST['zipcode'];
       echo "Zip data " . $zip;
    }
    if(isset($_POST['products']) && ($_POST['products'] != ""))
    {
        $products = $_POST['products'];
        echo "Products data " . $products;
    }
    if(isset($_POST['materials']) && ($_POST['materials'] != ""))
    {
        $materials = $_POST['materials'];
       echo "Materials data " . $materials;
    }
}
?>

By the way, you could use "if (!empty($_POST['myfield']))" instead of checking "isset" and then egal to empty.

Another thing : inputs are not supposed to be inside the label, so you should have :

<label for="myfield">My field :</label>
<input name="myfield" ... />

Upvotes: 1

Laurent S.
Laurent S.

Reputation: 6947

You simply shouldn't use elseif's as it will only pass in one of those statements, the first actually as you noticed. Use if's instead...

Edit : see naincy gupta's answer which shows exactly what I meant :-)

Upvotes: 0

Naincy
Naincy

Reputation: 2943

You can do it like

<?php
if (!isset($_POST['submit'])){
        echo "Nothing Selected";
}
else {
        if(isset($_POST['zipcode']) && ($_POST['zipcode'] != "")) {
          $zip = $_POST['zipcode'];
          echo "Zip data " . $zip;
        }

        if(isset($_POST['products'])  && ($_POST['products'] != "")) {
          $products = $_POST['products'];
          echo "Products data " . $products;
        }
        if(isset($_POST['materials'])   && ($_POST['materials'] != "")) {
          $materials = $_POST['materials'];
          echo "Materials data " . $materials;
        }
 }
?>

Upvotes: 1

Related Questions