August 17, 2011

Making an option in an HTML dropdown menu selected.

Question by Mason240

Thanks for reading my question.

I have a website that uses PHP for a searchable card database. Right now I have the code for the head/searchbox section on each page, but I want to use functions instead. The function itself is working. The problem is with the drop down boxes. I currently have it set up so that when a user selects and option, and then searches, the selections are selected on the next page.

I am sure that I am having a problem with syntax.

Here is an example of the working code, which is used on the live site right now.

    <select name="Type" onchange="this.submit()">
        <option value="1" >[All Card Types] </option>
        <option value="Hero" <?php if($_GET['Type'] == "Hero") echo "selected='selected'"; ?>> Hero </option>
        <option value="Ally" <?php if($_GET['Type'] == "Ally") echo "selected='selected'"; ?>> Ally </option>
    </select>

This is the relevant code from the function, which is not working, and is on the test site (ignore the error):

function searchBox(){
//Cell 1
    echo '
        <select name="Type" onchange="this.submit()">
            <option value="1" >[All Card Types] </option>
            <option value="Hero"  <?php if($_GET["Type"] == "Hero") { echo "selected=selected" ; }?>>   Hero    </option>
            <option value="Event" <?php if($_GET["Type"] == "Event") { echo "selected=selected"; }?>>   Event </option>
        </select>
    ';
}

As you can see in the test page, the dropdown menu doesn’t function like it does on the live page.

Thanks for the help!

Answer by Jonah

You can’t embed <?php tags in a string like that. You have to concatenate it with ternary operators.

function searchBox(){
//Cell 1
    echo '
        <select name="Type" onchange="this.submit()">
            <option value="1" >[All Card Types] </option>
            <option value="Hero"' . ($_GET['Type'] == 'Hero' ? ' selected=""' : '') . '>   Hero    </option>
            <option value="Event"' . ($_GET['Type'] == 'Event' ? ' selected=""' : '') . '>   Event </option>
        </select>
    ';
}

But for the sake of maintainability, you might do something more like this:

function searchBox() {
    $types = array('Hero', 'Event');
    $output = '<select name="Type" onchange="this.submit()">';
    $output .= ' <option value="1" >[All Card Types] </option>';
    foreach ($types as $type) {
        $output .= '<option value="' . $type . '"' . ($_GET['Type'] == $type ? ' selected=""' : '') . '>    ' . $type . '    </option>';
    }
    $output .= '</select>';
    echo $output;
}

Answer by Starx

You are missing the quotes

echo "selected="selected""

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!