views:

351

answers:

3

How can I get this code to show which option is already selected?

This is basically an edit page and it is pulling info from the database and populating the relative fields

I have a drop-down menu, multiple select box, and radio buttons on a page along with some elements. The info is getting displayed in the elements fine, but I can't work out how to get the s and radio buttons to display selected if they match the info from the database.

code:

<select name="client">


    <option value="empty">Change Client...</option>
 <?php
                $result2 = mysql_query("SELECT name FROM clients") or die("Database query failed: " . mysql_error());    

while($row = mysql_fetch_assoc($result2)) {
    $clientlist = $row['name'];
    $clientname = htmlspecialchars($row['name']);

    if ($_POST['client'] == $clientlist)
    { 

    echo '<option value="' . $clientlist . '" selected="selected" >' . $clientname . '</option>' . '\n';
    }
    else{
    echo '<option value="' . $clientlist . '" >' . $clientname . '</option>' . '\n';
}
}


?>
</select>

    </p>
<p class="subheadsmall">Core Classification</p>

<?php

switch ($niche) {
    case "brand":
        echo '<input type="radio" name="niche" value="Brand" checked="checked" />Brand';
        echo '<input type="radio" name="niche" value="Marketing" />Marketing';
        echo '<input type="radio" name="niche" value="Communication" />Communication';
        break;
    case "marketing":
        echo '<input type="radio" name="niche" value="Brand" />Brand';
        echo '<input type="radio" name="niche" value="Marketing" checked="checked" />Marketing';
        echo '<input type="radio" name="niche" value="Communication" />Communication';
        break;
    case "communication":
        echo '<input type="radio" name="niche" value="Brand" />Brand';
        echo '<input type="radio" name="niche" value="Marketing" />Marketing';
        echo '<input type="radio" name="niche" value="Communication" checked="checked" />Communication';
        break;
    default;
        echo '<input type="radio" name="niche" value="Brand" />Brand';
        echo '<input type="radio" name="niche" value="Marketing" />Marketing';
        echo '<input type="radio" name="niche" value="Communication" />Communication';
    break;
}

?>

<p class="subheadsmall">Strategies</p>

<p class="sidebargrey">

<?php

      $result = mysql_query("SELECT strategies FROM studies WHERE id = '$id';
       if (!$result) {
        die("Database query failed: " . mysql_error());
       }

while($row = mysql_fetch_array($result)) {
    $strategyname = $row['strategies'];


    echo $strategyname.'<br />';
}

?>
        <p class="subheadsmall">Add a strategy... (hold down command key to select more than one)</p>

<select name="strategies[]" multiple="multiple">
     <?php

      $result = mysql_query("SELECT * FROM strategies");
       if (!$result) {
        die("Database query failed: " . mysql_error());
       }

while($row = mysql_fetch_array($result)) {
    $strategylist = $row['name'];
    $strategyname = htmlspecialchars($row['name']);
$pagelink = str_replace(" ","_",$strategylist);

    echo '<option value="<a href=&quot;strategies.php?strategy=' . $pagelink . '&quot;>'.$strategyname.'</a>" >' . $strategyname . '</option>' . '\n';
}

?>
    </p>
A: 

OPTION HTML Spec

Change selected="selected" to just selected. Looks like that attribute doesn't need an assignment.

You might also want to check the HTML that's being output just to make sure your assignment is evaluating to true.

opedog
A: 

You could use javascript to do this. My example uses jquery

First give each of your checkboxes an id so

echo '<input type="radio" name="niche" id="brand" value="Brand" />Brand';
echo '<input type="radio" name="niche" id="marketing" value="Marketing" />Marketing';
echo '<input type="radio" name="niche" id="communication" value="Communication" />Communication';

Then your JS would be

$( "brand" ).attr( "checked", true ); // this would check the brand box

So you can just write out these as needed.

neilc
In case the OP is unaware, this JS requires jquery.
Shadow
A: 

You should -- if possible -- change that group of radio prints to a for loop. Then you can do something like this:

foreach ($possibleRadios as $key => $val)
{ 
    echo '<input type="radio" name="' . $val->name . '" value="' . $val->id . '" ' .  ($isSelected($val->id) ? 'selected="selected' : '') . ' />$val->prettyName';
}
Zack