Jump to content

select problem


airdee

Recommended Posts

hi all! i have this code that when a user selects a state, another select option will show below showing the counties or area of the selected state. however, i wanted the state to be selected (...selected>)when the user selects a state. currently, its returning to 'please select' option when i selected something. please help on how to do accomplish this.
                    <tr>
                      <td class="inputlbl"><span class="inputlblr">*</span>State:</td>
                      <td width="65%">
                      <?
                      $result=$db->query("select * from states");
                      echo "<select class=\"select\" onchange=\"window.location = this.options[this.selectedIndex].value\" name=\"state\">\n";
                      echo "<option value=\"\">Please select</option>\n";
            while($row=mysql_fetch_object($result)){
                $id=$row->id;
                $name=$row->name;
                $state=$_GET['state'];
                echo "<option value=\"content.php?page=buyers&state=$id\">$name</option>\n";
            }   
                      echo "</select>";
                      ?>
              </td></tr>
                    <?
                    if(isset($_GET['page']) && isset($_GET['state'])){
                        $state=$_GET['state'];
                    ?>   
                    <tr>
                      <td class="inputlbl"><span class="inputlblr">*</span>Area</td>
                      <td width="65%">
                      <?
                      $result=$db->query("select * from county where stateid=".$state."");
                      echo "<select class=\"select\" onchange=\"window.location = this.options[this.selectedIndex].value\" name=\"state\">\n";
            while($row=mysql_fetch_object($result)){
                $id=$row->id;
                $name=$row->name;
                echo "<option value=\"content.php?page=buyers&state=$state&area=$id\">$name</option>\n";
            }   
                      echo "</select>";
                      ?>
              </td></tr>
              <? } ?>
Link to comment
https://forums.phpfreaks.com/topic/25924-select-problem/
Share on other sites

When you echo out the state options, you've got to see if one has already been selected. Typically, it's a good idea to do all your variable checking at the top of the page so you can use the same check throughout the page instead of having to check for a variable in multiple locations.

For instance:
[code]
<?php
// First, set a variable for the selected state
// Default the value to nothing so we know it hasn't been selected
$state = isset($_GET['state']) ? $_GET['state'] : '';

// When you echo your state dropdown, just check to see a match
while ($row = mysql_fetch_object($result)) {
  $id  = $row->id;
  $name = $row->name;
  echo "<option value=\"content.php?page=buyers&state=$id\"";
  echo $state == $id ? ' selected="selected"' : '';
  echo ">$name</option>\n";
}
?>
[/code]

That added line of code will check to see if the state that has been selected matches the one that is currently being output, and if it does, it will mark it as selected for you.

Hope this helps.
Link to comment
https://forums.phpfreaks.com/topic/25924-select-problem/#findComment-118402
Share on other sites

That makes sense obsidian. Thank you!

however, i am seeing this ? and : but i dont understand these signs and what they do. can you please suggest me a url/link on where to learn these? thank you very much for your assisstance.

$state = isset($_GET['state']) ? $_GET['state'] : '';
Link to comment
https://forums.phpfreaks.com/topic/25924-select-problem/#findComment-118923
Share on other sites

[quote author=airdee link=topic=113593.msg462206#msg462206 date=1162525971]
however, i am seeing this ? and : but i dont understand these signs and what they do. can you please suggest me a url/link on where to learn these? thank you very much for your assisstance.
[/quote]

This is the [url=http://www.php.net/ternary]Ternary Operator[/url]. Scroll about a quarter of the way down that manual page I linked to (just below [b]Comparison Operators[/b]), and you'll see it. It's basically a shorthand way of writing this:
[code]
<?php
// instead of this
if (isset($_GET['state'])) {
  $state = $_GET['state'];
} else {
  $state = '';
}

// we write this
$state = isset($_GET['state']) ? $_GET['state'] : '';
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/25924-select-problem/#findComment-119098
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.