Mason240
Mason240

Reputation: 3044

Making an option in an HTML dropdown menu selected.

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!

Upvotes: 0

Views: 1127

Answers (4)

Jonah
Jonah

Reputation: 10091

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;
}

Upvotes: 3

Joe Landsman
Joe Landsman

Reputation: 2177

The problem is that you are outputting PHP as part of your literal text in the function. You need to rework the function so that the PHP logic is outside of the echo statement. In this case it may be easiest to jump in/out of PHP processing like this:

<?php function searchBox() { ?>
      <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>
<?php } ?>

Upvotes: 2

user557846
user557846

Reputation:

little messy but should work:

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

if($_GET["Type"]=="Hero"){
    echo "selected=selected";
}

echo '>Hero</option><option value="Event"';

if($_GET["Type"]=="Event"){
    echo "selected=selected";
}

echo '>Event</option></select>';
}

Upvotes: 1

Starx
Starx

Reputation: 79031

You are missing the quotes

echo "selected=\"selected\""

Upvotes: 1

Related Questions