1

I have an issue getting variables using PHP in a tablet site I have been working on. I have a search form on the homepage that passes variables to a list page. I then have the same search form in a 'refine search' dialog box which should pre-select the appropriate values depending on what has been passed, using PHP.

The problem is I can't seem to get the variables that have been passed (using PHP). For example I have this field in my search:

<select name="propertyType" id="propertyType">
        <option value="">Any Type</option>
        <option value="1"<?php if(isset($_GET['propertyType']) && $_GET['propertyType']=="1") { echo ' selected'; } ?>>Houses</option>
        <option value="2"<?php if(isset($_GET['propertyType']) && $_GET['propertyType']=="2") { echo ' selected'; } ?>>Flats/Apartments</option>
        <option value="3"<?php if(isset($_GET['propertyType']) && $_GET['propertyType']=="3") { echo ' selected'; } ?>>Bungalows</option>
        <option value="4"<?php if(isset($_GET['propertyType']) && $_GET['propertyType']=="4") { echo ' selected'; } ?>>Other</option>
    </select>

But when I pass any of these values my code does not pick them up and echo the relevant 'selected'.

The tablet site can be found here: http://muskermcintyre.co.uk/tablet Any help would be much appreciated!

Thanks.

aboother
  • 41
  • 1

2 Answers2

1

I suggest you following syntax:

<?php
    $values  = array(
        0 => 'Any Type',
        1 => 'Houses',
        2 => 'Flats/Apartments',
        3 => 'Bungalows',
        4 => 'Other'
    );
    $current = (int) $_GET['propertyType'];
?>

<select name="propertyType" id="propertyType">
<?php
    foreach ( $values as $key => $value ) {
        $selected = $key == $current ? 'selected="selected"' : '';
        echo "<option value='$key' $selected >$value</option>";
    }
?>
</select>
hsz
  • 148,279
  • 62
  • 259
  • 315
  • @Alvin it's when you've clicked 'list search' and then click the 'property search' button in the top left, that it doesn't then pick the variables up in the form that appears in the dialog box. Any ideas would be appreciated. – aboother Jul 24 '12 at 13:50
  • thanks for your reply, your suggestion didn't seem to work though I'm afraid – aboother Jul 24 '12 at 13:52
  • Try to `var_dump($_GET)` and look at the output. Does it returns `propertyType` value ? – hsz Jul 24 '12 at 14:42
  • Hi @hsz, it didn't return propertyType. It returned: array(0) { } – aboother Jul 24 '12 at 15:08
  • @aboother So it means that you do not pass any `propertyType` in your script. Try manually pass it with adding to URL: `?propertyType=2`. – hsz Jul 25 '12 at 07:17
0

I've had a similar problem in the past and got round it by getting the query string using Javascript/jQuery.

A quick search gave me this which might help you if you go down a similar route:

How can I get query string values in JavaScript?

Community
  • 1
  • 1
BIOSTALL
  • 1,656
  • 12
  • 25