PHP Post Working with Fields -
i have 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 zip code: <input maxlength="5" name="zipcode" size="6" type="text" /></label> <label>filter 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 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 posting data take value of first item selected.
i have tried breaking if ... elseif , doing redundant statements.
ie:
elseif(isset($_post['materials']) && ($_post['materials'] != "") || ($_post['products']) && ($_post['products'] != "") || $_post['zipcode']) && ($_post['zipcode'] != "")) {
that seems complicated , cannot believe there not easier way.
i have tried using switch ... case same results.
what im looking if 2 filters selected?
what if 3 selected?
how can data post out correctly.
you're checking if "zipcode" posted, , if it's not, if "products" posted, etc...
you should change "else if"s "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 way, use "if (!empty($_post['myfield']))" instead of checking "isset" , egal empty.
another thing : inputs not supposed inside label, should have :
<label for="myfield">my field :</label> <input name="myfield" ... />
Comments
Post a Comment