Hi
Firstly I apologise if this is the wrong forum. The code is a mix of PHP and Javascript, but my overall page is PHP.

What I have is one dropdown box, which depending on what is selected produces the appropriate second dropdown box. This is all great and works beautifully.

What I'm struggling to do is to make the selected options 'sticky'so that they remain in the search form. All my other fields in the form are sticky but I can't work it out for these fields as the second box is always hidden.

When the user clicks on 'submit' these 2 fields default back to 'Please Select'and the second box disappears from view. The user has to reselect every time to do a new search.

<select name="prop_type" onchange="display(this,'Private Apartments','Landed Property', 'Hdb Flat', 'Commercial', 'Hudc Apartments');">
                    <option selected="selected">Please Select</option>
                    <option value="Private Apartments">Private Apartments</option>
                    <option value="Landed Property">Landed Property</option>
                    <option value="Hdb Flat">Hdb Flat</option>
                    <option value="Commercial">Commercial </option>
                    <option value="Hudc Apartments">Hudc Apartments </option>
                    <option value="invisible">Invisible</option>
                  </select></td>
                </tr>
                <tbody id="Private Apartments" style="display: none;">
                  <tr>
                    <td colspan="2"><select name="prop_saletype1" id="prop_saletype1">
                      <?php
do {  
?>
                      <option value="<?php echo $row_prop_apartment['prop_saletype']?>"><?php echo $row_prop_apartment['prop_saletype']?></option>
                      <?php
} while ($row_prop_apartment = mysql_fetch_assoc($prop_apartment));
  $rows = mysql_num_rows($prop_apartment);
  if($rows > 0) {
      mysql_data_seek($prop_apartment, 0);
	  $row_prop_apartment = mysql_fetch_assoc($prop_apartment);
  }
?>
                    </select></td>
                  </tr>
                </tbody>
                <tbody id="Landed Property" style="display: none;">
                  <tr>
                  <td colspan="2"><select name="prop_saletype2" id="prop_saletype2">
                    <?php
do {  
?>
                    <option value="<?php echo $row_prop_landed['prop_saletype']?>"><?php echo $row_prop_landed['prop_saletype']?></option>
                    <?php
} while ($row_prop_landed = mysql_fetch_assoc($prop_landed));
  $rows = mysql_num_rows($prop_landed);
  if($rows > 0) {
      mysql_data_seek($prop_landed, 0);
	  $row_prop_landed = mysql_fetch_assoc($prop_landed);
  }
?>
                  </select></td> </tr>
                  </tbody>

Any help would be great! Many thanks liz

Recommended Answers

All 12 Replies

I am assuming your method is POST, otherwise you may change it to GET

<select name="prop_type" onchange="display(this,'Private Apartments','Landed Property', 'Hdb Flat', 'Commercial', 'Hudc Apartments');">

<option >Please Select</option>

<option value="Private Apartments" <?php echo ($_POST['prop_type']=="Private Apartments")?"selected":"";?>>Private Apartments</option>

<option value="Landed Property" <?php echo ($_POST['prop_type']=="Landed Property")?"selected":"";?>>Landed Property</option>

<option value="Hdb Flat" <?php echo ($_POST['prop_type']=="Hdb Flat")?"selected":"";?>>Hdb Flat</option>

<option value="Commercial" <?php echo ($_POST['prop_type']=="Commercial")?"selected":"";?>>Commercial </option>

<option value="Hudc Apartments" <?php echo ($_POST['prop_type']=="Hudc Apartments")?"selected":"";?>>Hudc Apartments </option>

<option value="invisible" <?php echo ($_POST['prop_type']=="invisible")?"selected":"";?>>Invisible</option>


</select>

Hi Yes the form method is POST. Would using GET solve this? I'm not really understand why then? many thanks

No, but if you used GET, you would have to use $_GET instead of $_POST.

I assume that you are submitting to self page.

Member Avatar for diafol

I'd use JS (jQuery to do this).

pass the $_POST var on to js on page load. Get js to change the selected object. No need to waste all that lovely php on conditionals:

First of all give the select tag an id:

<select id="prop_type" name="prop_type" onchange="display(this,'Private Apartments','Landed Property', 'Hdb Flat', 'Commercial', 'Hudc Apartments');">

Personally, I'd get rid of the inline js and put it in the head. I have no idea what this display function is supposed to do, but it looks wasteful. ANyway...

...include jquery file from local or google cdn...
<script>
...
$('document).ready(function(){
<?php
if(isset($_POST) && !empty($_POST)){
?>
    $("#prop_type").val('<?php echo $_POST['prop_type'];?>');
<?php
}
?>
});
...
</script>

Why would you use javascript for that? I think it's not really nice for the 5% that don't have javascript to do this while you can do it equally as easy with php. Also, why would you have the client download and load jQuery? It seems useless to me.

Member Avatar for diafol

I agree with the x% (is it still 5%?) who don't have js and I must admit I usually put a caveat with these things. Anyway I would use this to enable scalability. The fact that the OP is already using js was my go-ahead. Admittedly, jQuery may be overkill, but js retards like me find it more manageable.

[OK what follows is my rationale - I not lecturing :)]

Dropdown data can be stored in DBs or arrays and created dynamically (that's how I create mine - usually). In which case

<option value="Hdb Flat" <?php echo ($_POST['prop_type']=="Hdb Flat")?"selected":"";?>>Hdb Flat</option>

this does not seem to be so long winded! as you could do this for all of them via loop

$sel = ($array[$x] == $_POST[...]) ? ' selected="selected"' : '';
echo "<option value=\"{array[$x]}\"{$sel}>{array[$x]}</option>";

But it seems the OP is hard-coding all the values and having to apply all the conditionals manually. Nosebleed! (for me anyway ;))

The js method just facilitates the setting of the selection 'automatically'. So you add, or edit the 'php-less' options knowing that the selected option will be displayed after form sent.

Hope I've justified my useless suggestion.

Hi
Many thanks for all your input. Actually JS/Javascript is all quite new to me.
I've only hard-coded the first dropdown, the dropdowns that appear depending on what you select in the first are lookups to a mysql table.

This is the functioning part of the code at the top of page:

<script type="text/javascript">
// <![CDATA[
function display(obj,id1,id2, id3, id4, id5) {
txt = obj.options[obj.selectedIndex].value;
document.getElementById(id1).style.display = 'none';
document.getElementById(id2).style.display = 'none';
document.getElementById(id3).style.display = 'none';
document.getElementById(id4).style.display = 'none';
document.getElementById(id5).style.display = 'none';
if ( txt.match(id1) ) {
document.getElementById(id1).style.display = 'block';
}
if ( txt.match(id2) ) {
document.getElementById(id2).style.display = 'block';
}
if ( txt.match(id3) ) {
document.getElementById(id3).style.display = 'block';
}
if ( txt.match(id4) ) {
document.getElementById(id4).style.display = 'block';
}
if ( txt.match(id5) ) {
document.getElementById(id5).style.display = 'block';
}
}

It is both drop downs that need to be sticky.

I'd prefer to stick with PHP if possible, only because I'm more familiar with it now...I'll try the above options.

Many thanks for the input. Much appreciated.

So now I just need to work out how to keep the 2nd drop down sticky

This is where I get stuck - when the field is dynamically looking up data from a table:

<option value="<?php echo $row_prop_apartment['prop_saletype']?>"><?php echo $row_prop_apartment['prop_saletype']?></option>

There must be somewhere in here that will keep the field showing even with the javasript hiding each option at a time?

It should be:

<option value="<?= $row_prop_apartment['prop_saletype'] ?>"<?= $row_prop_apartment['prop_saletype'] == $the_value_in_the_database ? ' selected="selected"' : '' ?>><?= $row_prop_apartment['prop_saletype'] ?>
</option>
Member Avatar for diafol

Using short tags could cause a problem. <?= to <?php echo may be better.

I know, but if it is you could just do a quick replace.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.