Jump to content

php within html within php


perky416

Recommended Posts

Hi Guys

 

On my website i have a page where the user can edit their profile. I have a drop down box that is only echoed if the user is listed as female in the database.

 

<form>
<?php
$db_dress_size = $row['dress_size'];
if ($row['gender'] == "Female")
{	echo "
Dress Size:
<select name='dress_size'>
<option>2</option>
</select>"
}
?>
</form>

 

I need to add the following piece of php within the option tag, however i cant figure out how to get it to work.

 

<?php if ($submit) { if ($dress_size == '2') { echo 'selected';}} else { if  ($db_dress_size == '2') {echo 'selected';}}  ?>

 

The above code works fine for a standard html drop down box, however as the html is already within php it is not working. When i view the source code the added php is also echoed.

 

Please could someone help me figure out how solve the issue.

 

Thanks

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/229767-php-within-html-within-php/
Share on other sites

Well you have two options. Either echo the HTML outside of PHP:

 

<?php
$db_dress_size = $row['dress_size'];
if ($row['gender'] == "Female")
{	
?>Dress Size:
<select name='dress_size'>
<option<?php if ($submit) { if ($dress_size == '2') { echo ' selected';}} else { if  ($db_dress_size == '2') {echo ' selected';}}  ?>>2</option>
</select>
<?php
}
?>

 

Or use it as a variable in the echo:

 

<?php
$db_dress_size = $row['dress_size'];
if ($row['gender'] == "Female")
{	
$selected = "";
if ($submit) { if ($dress_size == '2') { $selected = ' selected';}} else { if  ($db_dress_size == '2') {$selected = ' selected';}}
echo "
Dress Size:
<select name='dress_size'>
<option".$selected.">2</option>
</select>"
}
?>

Either echo the HTML outside of PHP:

 

<?php
$db_dress_size = $row['dress_size'];
if ($row['gender'] == "Female")
{	
?>Dress Size:
<select name='dress_size'>
<option<?php if ($submit) { if ($dress_size == '2') { echo ' selected';}} else { if  ($db_dress_size == '2') {echo ' selected';}}  ?>>2</option>
</select>
<?php
}
?>

 

Thank you mattal999 that worked perfectly!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.