0

Hi I have a piece of php code which takes php variables from a database and then populates A drop down box. This works fine but once the page updates it doesn't display the selected value and instead just displays the original list again.

I have played around with some different ideas of using isset to displays the selected value, but then it just displays that value and nothing else.

I have attached the original code where it works but always only shows the original list of values from the database.

If any body could provide a solution or even a nudge in the right direction then it would be much appreciated.

Thanks

if (isset($select) && $select != "location") {
    $select = $_POST['location'];
}
?>

<select name="location">
<?php
// Get records from database (table "name_list").
$list = mysql_query("select DISTINCT region_name from masterip_details WHERE country_code='GB' AND TRIM(IFNULL(region_name,'')) <> '' order by region_name asc");

// Show records by while loop.
while ($row_list = mysql_fetch_assoc($list)) {
    $location = $_POST['location']; ?>
    <option value="<?php echo $row_list['region_name']; ?>"
    <?php if  $row_list['region_name'] == $select) {
        echo "selected";
    } ?>><?php echo $row_list['region_name']; ?></option>        
<?php } ?>
7
  • where is $select set? Commented Jul 18, 2013 at 9:47
  • Shouldn't it be && $select !== "location"? Commented Jul 18, 2013 at 9:49
  • @Alfo not compulsorily Commented Jul 18, 2013 at 9:50
  • $select is set in the 2nd line! Commented Jul 18, 2013 at 9:51
  • 1
    Check $_POST['location'] has any of one dropdown box values. Commented Jul 18, 2013 at 9:51

4 Answers 4

1

You are sure you send that form with a post request?

Your code is unreadable Better do something like this:

<?php

    //database request don't belong in the presentation layer
    $list=mysql_query("select DISTINCT region_name from masterip_details WHERE country_code='GB' AND TRIM(IFNULL(region_name,'')) <> '' order by region_name asc");
    $location = "";
    $isSelect = "";
    if(isset($_POST['location'])){
        $location = $_POST['location']; 
    }
    while($row_list=mysql_fetch_assoc($list)){
        if($row_list['region_name'] == $location ){ 
            $isSelect = "selected"; 
         } 
         echo '<option value="'.$row_list['region_name'].'" '.$isSelect.'  >'. $row_list['region_name'].'</option>';

    }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I like the code much neater and easier to follow though it should an error around If(if($row ... But i will look at this and get it working as it is much neater than my way.
1

You are not checking the select value properly.

Change this:

if (isset($select) && $select != "location") {
    $select = $_POST['location'];
}

TO

if (isset($_POST['location']) && $_POST['location'] != "location") {
    $select = $_POST['location'];
}

Comments

0

While creating the <option> tag, you may use the following:

<?php if ($row_list['region_name'] == $select) { echo "selected='true'"; } ?>

You may also like to check if the condition $row_list['region_name'] == $select is returning true. You may try to echo their result separately as:

echo ($row_list['region_name'] == $select)

or something similar that would show if you get true or not.

2 Comments

@Ranjith, yes it is not necessary to set it like this. The issue could be with $row_list['region_name'] == $select also. But adhering to XHTML conventions, every attribute should have a value.
Yes, you are right. But here its not the major problem. refer this w3.org/community/webed/wiki/HTML/Elements/option#Example_A link of w3.org
0

The problem lies in the first line - actually the system will never execute 2nd line to assign $select because $select is never set:

if(isset($_POST['select']) && $select!="location") { $select=$_POST['location']; } ?>

1 Comment

and also the bit $location = $_POST['location'] in the while loop is not making sense...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.