perky416 Posted March 6, 2011 Share Posted March 6, 2011 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 More sharing options...
mattal999 Posted March 6, 2011 Share Posted March 6, 2011 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>" } ?> Link to comment https://forums.phpfreaks.com/topic/229767-php-within-html-within-php/#findComment-1183537 Share on other sites More sharing options...
perky416 Posted March 6, 2011 Author Share Posted March 6, 2011 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! Link to comment https://forums.phpfreaks.com/topic/229767-php-within-html-within-php/#findComment-1183540 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.