PHP Form: Replacing selected value with another value
Posted: Sun Mar 11, 2012 4:20 am
Hello,
I ran into a wall with my code that is beyond my knowledge. Any help is appreciated. Thanks!!!!
I have this as the output:
Form #1
<select name="form1">
<option value="LA">Los Angeles</option>
<option value="SF" selected="selected">San Francisco</option>
<option value="No">New Orleans</option>
</select>
Form #2
<select name="form2">
<option value="LA">Los Angeles</option>
<option value="ReplaceMe" selected="selected">San Francisco</option>
<option value="No">New Orleans</option>
</select>
Form1 has the desired effect but in Form2 I need to replace the selected value with a different string. This new string will be the same no matter what the user selects in Form2. I tried using str_replace but I'm not doing it right.
Here's my code to dynamically populate the menu and get selected item:
function getCities()
{
$city = array
(
'LA' => 'Los Angeles',
'SF' => 'San Francisco',
'NO' => 'New Orleans
);
foreach($city as $key => $value) {
$selected = '';
if(isset($_POST 'form1') && $_POST 'form1' == $value)
{
$selected = ' selected="selected"';
}
echo '<option value="'.$key.'"'.$selected.'>'.$value.'</option>'. "\n";
}
}
My attempt at using str_replace to replace the value selected in Form2:
//find string
$newstring = "<option value=\"replaceme\" selected=\"selected\">";
//replace
$valuestr = str_replace("replaceme", "This is the new selected value for Form2", $newstring);
//getCities function - not sure how to override or add to function to replace the string inside the value, conditionals needed?
getCities($valuestr);
I ran into a wall with my code that is beyond my knowledge. Any help is appreciated. Thanks!!!!
I have this as the output:
Form #1
<select name="form1">
<option value="LA">Los Angeles</option>
<option value="SF" selected="selected">San Francisco</option>
<option value="No">New Orleans</option>
</select>
Form #2
<select name="form2">
<option value="LA">Los Angeles</option>
<option value="ReplaceMe" selected="selected">San Francisco</option>
<option value="No">New Orleans</option>
</select>
Form1 has the desired effect but in Form2 I need to replace the selected value with a different string. This new string will be the same no matter what the user selects in Form2. I tried using str_replace but I'm not doing it right.
Here's my code to dynamically populate the menu and get selected item:
function getCities()
{
$city = array
(
'LA' => 'Los Angeles',
'SF' => 'San Francisco',
'NO' => 'New Orleans
);
foreach($city as $key => $value) {
$selected = '';
if(isset($_POST 'form1') && $_POST 'form1' == $value)
{
$selected = ' selected="selected"';
}
echo '<option value="'.$key.'"'.$selected.'>'.$value.'</option>'. "\n";
}
}
My attempt at using str_replace to replace the value selected in Form2:
//find string
$newstring = "<option value=\"replaceme\" selected=\"selected\">";
//replace
$valuestr = str_replace("replaceme", "This is the new selected value for Form2", $newstring);
//getCities function - not sure how to override or add to function to replace the string inside the value, conditionals needed?
getCities($valuestr);