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);
PHP Form: Replacing selected value with another value
Moderator: General Moderators
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: PHP Form: Replacing selected value with another value
You use of the function seems to be correct; what results are you getting?notaphper wrote:but I'm not doing it right.
PHP Manual wrote:mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
Since you're function requires no arguments you don't have to pass any; by calling the function (minus $valuestr) it will be parsed and the code within the function is executednotaphper wrote://getCities function - not sure how to override or add to function to replace the string inside the value, conditionals needed?
getCities($valuestr);
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: PHP Form: Replacing selected value with another value
Hi,
Thanks for your reply.
Say I have this: <option value="Jan" selected="selected">January</option>
But now I want to replace the value: <option value="different value string" selected="selected">January</option>
I have no problem getting the selected value by calling the function, but I just can't replace the string inside the value once it's selected. Do I need an if statement after my function is called then do a str_repace? I can't wrap my head around the logic. Thanks!
Thanks for your reply.
Say I have this: <option value="Jan" selected="selected">January</option>
But now I want to replace the value: <option value="different value string" selected="selected">January</option>
I have no problem getting the selected value by calling the function, but I just can't replace the string inside the value once it's selected. Do I need an if statement after my function is called then do a str_repace? I can't wrap my head around the logic. Thanks!
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: PHP Form: Replacing selected value with another value
Should the code execute only if something specific happens? This is what determines whether an if-statement is needed. can you paste the code you are using at the momentnotaphper wrote:Do I need an if statement after my function is called then do a str_repace? I can't wrap my head around the logic.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering