Forum Moderators: coopster

Message Too Old, No Replies

str_replace help

         

electricocean

7:00 am on Jul 5, 2005 (gmt 0)

10+ Year Member



How do you output what I have replaced w/ str_replace?

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

maxi million

8:52 am on Jul 5, 2005 (gmt 0)

10+ Year Member



May be this wont be very helpful, but if I had similar error i would assign $_POST['genre'] to a variable:
ie
$genre = $_POST['genre'];
and in line 81 replace $_POST['genre'] with $genre just to make sure

tomda

9:00 am on Jul 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maximillion is right, it is a string problem, therefore you can define a genre variable to be sure.

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

electricocean

9:56 pm on Jul 5, 2005 (gmt 0)

10+ Year Member



Thanks everyone that fixed the error. Now When I say Preview it doesn't even show a pulldown menu, do have any idea whats wrong?

electricocean

electricocean

7:24 pm on Jul 9, 2005 (gmt 0)

10+ Year Member



Can someone please help, this is pretty serious

thanks,
electricocean

Birdman

7:57 pm on Jul 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is how I usually do this type of thing:

$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.

mcibor

8:05 pm on Jul 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this:

else{
$genre = $_POST["genre"];
$genre_pulldown = str_replace("$genre\"", "$genre\" selected", $genre_pulldown);
}

however I don't know what the problem could be...
Try and see the source code (in browser) and check what may be wrong.
Michal Cibor