Search code examples
phpisset

PHP Drop Down Boxes always Isset


HI i have two drop down boxes which are used to filter data from a mysql table, the problem is the boxes always show as if they are set when i use isset to determin if any of them have any values selected.

Here is the code for the drop boxes.

 <?php 
                       if (isset($_POST['referrer']) && $_POST['referrer'] != "referrer") {
                       $select = $_POST['referrer'];
                         }
                       ?>
                                    <select name="referrer">
                                    <option value="">Referrer</option>

                                      <?php
                       // Get records from database (table "name_list").
                       $list=mysql_query("select DISTINCT referrer from masterip_details WHERE country_code='GB' AND TRIM(IFNULL(referrer,'')) <> '' order by referrer DESC");
                       // Show records by while loop.
                       while($row_list=mysql_fetch_assoc($list)){
                       $referrer = $_POST['referrer']; ?>
                                      <option value="<?php echo $row_list['referrer']; ?>" <?php if($row_list['referrer']==$select){ echo "selected"; } ?>><?php echo $row_list['referrer']; ?></option>        
                                      <?php     
                       }
                       ?>
                                    </select>



 <?php 
                       if (isset($_POST['returning']) && $_POST['returning'] != "returning") {
                       $select2 = $_POST['returning'];
                         }
                       ?>
                                    <select name="returning">
                                    <option value="">Referrer</option>
                                      <?php
                       // Get records from database (table "name_list").
                       $list2=mysql_query("select * from repeater_drop_down order by id DESC");
                       // Show records by while loop.
                       while($row_list2=mysql_fetch_assoc($list2)){
                       $returning = $_POST['returning']; ?>
                                      <option value="<?php echo $row_list2['value']; ?>" <?php if($row_list2['value']==$select2){ echo "selected"; } ?>><?php echo $row_list2['title']; ?></option>     
                                      <?php     
                       }
                       ?>
                                    </select>

Now here are the isset elements of the code.

$returning = $_POST['returning'];
$referrer = $_POST['referrer'];

 if (isset($returning, $referrer)){

 //code //

  elseif (isset($returning)){

   ///code //

  elseif (isset($referrer)){

  //code //

  else {

 //code   

        }

I haven't included the queries as they work fine but whether or not i select both drop down boxes the code automatically goes to the first isset for both variable even if only one of the boxes has a option selected.

Any advice or suggestions would be appreciated.


Solution

  • You should check the value that was submitted with the form, not only whether or not the select POST value is set.

    The value of the referrer will be '' (an empty string) if it is submitted without changing the select. Check for that instead.