Page 1 of 1

PHP Form: Replacing selected value with another value

Posted: Sun Mar 11, 2012 4:20 am
by notaphper
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);

Re: PHP Form: Replacing selected value with another value

Posted: Sun Mar 11, 2012 4:49 pm
by social_experiment
notaphper wrote:but I'm not doing it right.
You use of the function seems to be correct; what results are you getting?
PHP Manual wrote:mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
notaphper wrote://getCities function - not sure how to override or add to function to replace the string inside the value, conditionals needed?
getCities($valuestr);
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 executed

Re: PHP Form: Replacing selected value with another value

Posted: Sun Mar 11, 2012 7:04 pm
by notaphper
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!

Re: PHP Form: Replacing selected value with another value

Posted: Mon Mar 12, 2012 12:44 am
by social_experiment
notaphper 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.
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 moment