Forum Moderators: coopster
I had this wacky idea to have a pull down that keeps the same values when previewing what they have select in the post (ie. Webmaster World's 'Preview' Button When you post-- It has the same value of what u put in it when you add some content on the page to show what you have submitted)
My Code:
$genre_pulldown = "<select name=\"genre\" id=\"genre\"><optgroup label=\"Genre\"><option value=\"Yo Mama\">Yo Mama</option><option value=\"Blonde\">Blonde</option><option value=\"Political\">Political</option><option value=\"Religious\">Religious</option><option value=\"knock Knock Jokes\">knock Knock Jokes</option></optgroup></select>";
if(!isset($_POST['genre'])){
echo $genre_pulldown;
}
else{
$genre_pulldown = str_replace('{$_POST['genre']}\"', '{$_POST['genre']}\" selected', '{$genre_pulldown}');//LINE 81
}
I always get this error though:
Parse error: parse error, unexpected T_STRING in /usr/export/www/hosting/dkicks/admin/add_a_joke.php on line 81
How do you output the replaced data?
Is there a better way to do this?
thanks,
electricocean
Also, note that you can both use single and double quotes and remove the slashed.
$genre_pulldown = '<select name="genre" id="genre"><optgroup label="Genre">
<option value="Yo Mama">Yo Mama</option>
<option value="Blonde">Blonde</option>
<option value="Political">Political</option>
<option value="Religious">Religious</option>
<option value="knock Knock Jokes">knock Knock Jokes</option></optgroup></select>';
Just to make your coding easier
$genres = array('Yo Mama','Blonde','Political','Religious','knock Knock Jokes');
$genre_drop = '';
foreach ($genres as $val) { //loop the array
//below is basically an if(), but shortened
$selected = ( isset($_POST['genre']) && $_POST['genre'] == $val )? ' selected' : '';
$genre_drop .= '<option'.$selected.'>'.$val.'</option>';
}
May be confusing at first, but you'll understand it after a bit.