beansandsausages Posted January 9, 2008 Share Posted January 9, 2008 Sorry if this is in the wrong topic. i have a question i have a script <select name=\"age\"> <option value=\"10\">10 - 15</option> <option value=\"15\">15 - 20</option> </select> is it possable to keep the value selected that the member has chosen for example : member is 15 - 20 so form would be : <select name=\"age\"> <option value=\"10\">10 - 15</option> <option value=\"15\" selected>15 - 20</option> </select> is there any way to do it from the db if you understand what i am trien to say ??? sorry if i dont make no sence no good at explaning. Quote Link to comment Share on other sites More sharing options...
kratsg Posted January 9, 2008 Share Posted January 9, 2008 Yes, there is. Here's a shorthand coding that's always worked for me (I was never any good at the shorthand style, but this one works ;-) <?php echo "<option value='10'".($age == '10' ? ' SELECTED' : '').">10-15</option>"; ?> Or <option value='10'<? ($age == '10' ? ' SELECTED' : '') ?>>10-15</option> Either one should be satisfactory. The code format works like this: (CONDITION ? ECHO TRUE : ECHO FALSE) It's a special case of the if/else conditions, but to just echo out something... It's mostly used in forms for auto-filling things in. Quote Link to comment Share on other sites More sharing options...
beansandsausages Posted January 9, 2008 Author Share Posted January 9, 2008 Dont work get no errors just doesnt select the correct one <select name=\"age\"> <option value=\"10\"".($age == '10' ? ' SELECTED' : '')."\">10 - 15</option> <option value=\"15\"".($age == '15' ? ' SELECTED' : '')."\">15 - 20</option> </select> Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted January 9, 2008 Share Posted January 9, 2008 post more of your code... its hard to tell.. Quote Link to comment Share on other sites More sharing options...
beansandsausages Posted January 9, 2008 Author Share Posted January 9, 2008 This is all the code other than the variables : <td width=\"15%\">$space First Name</td><td width=\"1%\">:</td><td width=\"9%\"><input type=\"text\" name=\"first_name\" value=\"".$first_name."\"></td> <tr><td width=\"15%\">$space Last Name</td><td width=\"1%\">:</td><td width=\"9%\"><input type=\"text\" name=\"last_name\" value=\"".$last_name."\"></td> <tr><td width=\"15%\">$space Email</td><td width=\"1%\">:</td><td width=\"9%\"><input type=\"text\" name=\"email\" value=\"".$email."\"></td> <tr><td width=\"15%\">$space Age</td><td width=\"1%\">:</td><td width=\"9%\"><select name=\"age\"><option value=\"10\"".($age == '10' ? 'SELECTED' : '')."\">10 - 15</option><option value=\"15\"".($age == '15' ? 'SELECTED' : '')."\">15 - 20</option><option value=\"20\"".($age == '20' ? 'SELECTED' : '')."\">20 - 25</option><option value=\"25\"".($age == '25' ? 'SELECTED' : '')."\">25+ </option></td> <tr><td width=\"15%\">$space Gender</td><td width=\"1%\">:</td><td width=\"9%\"><input type=\"checkbox\" name=\"gender\" value=\"male\" checked> Male $space $space $space<input type=\"checkbox\" name=\"gender\" value=\"female\"> Female</td></table> Quote Link to comment Share on other sites More sharing options...
kratsg Posted January 9, 2008 Share Posted January 9, 2008 What we need is the actual php code, not just the html. What variable holds the value for the age? Quote Link to comment Share on other sites More sharing options...
beansandsausages Posted January 9, 2008 Author Share Posted January 9, 2008 Full file is : <?php include_once('db.php'); $check = mysql_query("SELECT * FROM members WHERE username = '$username'")or die(mysql_error()); while($info = mysql_fetch_array( $check )) $username = $info['username']; $first_name = $info['first_name']; $email = $info['email']; $last_name = $info['last_name']; $age = $info['age']; echo " <form action=\"update.php\" method=\"post\"> <input type=\"hidden\" name=\"id\" value=\"".$id."\"> <table width=\"25%\"><td width=\"25%\">$space <strong><u>Personal Details</u></strong></td></table> <table width=\"25%\"> <td width=\"15%\">$space First Name</td><td width=\"1%\">:</td><td width=\"9%\"><input type=\"text\" name=\"first_name\" value=\"".$first_name."\"></td> <tr><td width=\"15%\">$space Last Name</td><td width=\"1%\">:</td><td width=\"9%\"><input type=\"text\" name=\"last_name\" value=\"".$last_name."\"></td> <tr><td width=\"15%\">$space Email</td><td width=\"1%\">:</td><td width=\"9%\"><input type=\"text\" name=\"email\" value=\"".$email."\"></td> <tr><td width=\"15%\">$space Age</td><td width=\"1%\">:</td><td width=\"9%\"><select name=\"age\"><option value=\"10\"".($age == '10' ? 'SELECTED' : '')."\">10 - 15</option><option value=\"15\"".($age == '15' ? 'SELECTED' : '')."\">15 - 20</option><option value=\"20\"".($age == '20' ? 'SELECTED' : '')."\">20 - 25</option><option value=\"25\"".($age == '25' ? 'SELECTED' : '')."\">25+ </option></td> <tr><td width=\"15%\">$space Gender</td><td width=\"1%\">:</td><td width=\"9%\"><input type=\"checkbox\" name=\"gender\" value=\"male\" checked> Male $space $space $space<input type=\"checkbox\" name=\"gender\" value=\"female\"> Female</td></table> "; ?> Quote Link to comment Share on other sites More sharing options...
kratsg Posted January 9, 2008 Share Posted January 9, 2008 Hmm, I want you to try something to see exactly what value we're getting out of the age. Echo the $age variable at the top of the page. Add some numbers in between the <select> so we know which one is activating or not activating (if any). I mean like this: .($age == '10' ? 'SELECTED' : '1'). .($age == '15' ? 'SELECTED' : '2'). //and so on.... Then, view the page, check the $age value, if it's null or doesn't exist, that's your problem. If it's a number that doesn't match with any of the option values, that's your problem. If it's a number that SHOULD match with one of the option values, but doesn't, then there's something else that we're missing. Quote Link to comment Share on other sites More sharing options...
revraz Posted January 9, 2008 Share Posted January 9, 2008 Is $age a INT? ($age == 10 ? 'SELECTED' : '') Quote Link to comment Share on other sites More sharing options...
beansandsausages Posted January 10, 2008 Author Share Posted January 10, 2008 yeah age is a int Quote Link to comment Share on other sites More sharing options...
kratsg Posted January 11, 2008 Share Posted January 11, 2008 yeah age is a int Then, remove the quotes around the numbers //INSTEAD OF ($age == '10' ? 'SELECTED' : '') //HAVE ($age == 10 ? 'SELECTED' : '') Quote Link to comment Share on other sites More sharing options...
beansandsausages Posted January 11, 2008 Author Share Posted January 11, 2008 Hey echoed out ".$age." and gives the correct value. and code <select name=\"age\"> <option value=\"10\" ($age == 10 ? 'SELECTED' : '')>10 - 15</option> <option value=\"15\" ($age == 15 ? 'SELECTED' : '')>15 - 20</option> <option value=\"20\" ($age == 20 ? 'SELECTED' : '')>20 - 25</option> <option value=\"25\" ($age == 25 ? 'SELECTED' : '')>25+</option> </select> Dosnt work sorry. Just in case you need age. $age = 20; Quote Link to comment Share on other sites More sharing options...
kratsg Posted January 11, 2008 Share Posted January 11, 2008 Hey echoed out ".$age." and gives the correct value. and code <select name=\"age\"> <option value=\"10\" ($age == 10 ? 'SELECTED' : '')>10 - 15</option> <option value=\"15\" ($age == 15 ? 'SELECTED' : '')>15 - 20</option> <option value=\"20\" ($age == 20 ? 'SELECTED' : '')>20 - 25</option> <option value=\"25\" ($age == 25 ? 'SELECTED' : '')>25+</option> </select> Dosnt work sorry. Just in case you need age. $age = 20; Ok, I wanna try to figure out if anything is even being echoed in the first place (I tried it on my server, seems to work w/o fail). <select name=\"age\"> <option value=\"10\" ($age == 10 ? 'SELECTED' : 'no1')>10 - 15</option> <option value=\"15\" ($age == 15 ? 'SELECTED' : 'no2')>15 - 20</option> <option value=\"20\" ($age == 20 ? 'SELECTED' : 'no3')>20 - 25</option> <option value=\"25\" ($age == 25 ? 'SELECTED' : 'no4')>25+</option> </select> The actual presentation probably will look funky, so just look at the source code and see what the above code would output for you. If we see all the no's, that means the $age is not equating correctly. Quote Link to comment Share on other sites More sharing options...
revraz Posted January 11, 2008 Share Posted January 11, 2008 option value should = 10, not "10" Quote Link to comment Share on other sites More sharing options...
kratsg Posted January 11, 2008 Share Posted January 11, 2008 option value should = 10, not "10" That wouldn't make a difference, especially if it's stored as an INT on the database. Quote Link to comment Share on other sites More sharing options...
beansandsausages Posted January 12, 2008 Author Share Posted January 12, 2008 actual sorce code is : Age</td><td width="1%">:</td><td width="9%"><select name="age"><option value=10 (25 == 10 ? 'SELECTED' : '')>10 - 15</option><option value=15 (25 == 15 ? 'SELECTED' : '')>15 - 20</option><option value=20 (25 == 20 ? 'SELECTED' : '')>20 - 25</option><option value=25 (25 == 25 ? 'SELECTED' : '')>25+</option></select> 25</td> Quote Link to comment Share on other sites More sharing options...
kratsg Posted January 12, 2008 Share Posted January 12, 2008 actual sorce code is : Age</td><td width="1%">:</td><td width="9%"><select name="age"><option value=10 (25 == 10 ? 'SELECTED' : '')>10 - 15</option><option value=15 (25 == 15 ? 'SELECTED' : '')>15 - 20</option><option value=20 (25 == 20 ? 'SELECTED' : '')>20 - 25</option><option value=25 (25 == 25 ? 'SELECTED' : '')>25+</option></select> 25</td> Whoa... that's odd... you see that when you view the HTML source code? Then the php is not even running in the first place.. o_O Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.