Accurax Posted January 10, 2007 Share Posted January 10, 2007 Hi guys,Ive managed to get my text fields and my drop down menu's to pre fill them selves depending upon the data that is in my database for each field, so im really happy about this.However, im having problems with radio buttions, and probably checkbox's when i get to them.Below is my form to allow someone to select thier gender, and id like it to pre check the correct radio button dependng on what the user previously entered (in case they change sex i guess).anyway, heres the code i have at the moment, id really appreciate some help on this, as logically i think this should work.... in my mind at least.[code]<input name="info_sex" type="radio" value="male" checked="<?php $username = $_SESSION['username'];$query = "SELECT sex FROM members WHERE user_name='$username'";$result = mysql_query($query) or die ("could not find sex");$row = mysql_fetch_array($result);$row['sex'] = $gender;if ($gender == "male"){echo $row['checked']; }?> "/><strong>A Male</strong><br /><input name="info_sex" type="radio" value="female" checked="<?php $username = $_SESSION['username'];$query = "SELECT sex FROM members WHERE user_name='$username'";$result = mysql_query($query) or die ("could not find sex");$row = mysql_fetch_array($result);$row['sex'] = $gender;if ($gender == "female"){echo $row['checked']; }?> "/><strong>A Female</strong><br />[/code] Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 10, 2007 Share Posted January 10, 2007 Look up HTML radio buttons. You'll need to add CHECKED to the correct one. Quote Link to comment Share on other sites More sharing options...
Accurax Posted January 10, 2007 Author Share Posted January 10, 2007 Errm... yes i know thatDid u actually read my post by the way? Quote Link to comment Share on other sites More sharing options...
Accurax Posted January 10, 2007 Author Share Posted January 10, 2007 In simple terms this is what i want to do1) check to see whether the database contains the string male or the string female2) check the corresponding boxThe above script feels logical to me, i may be approaching this from the wrong angle completely, or my php / sql syntax may be dodgy... id really appreciate some help though Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 10, 2007 Share Posted January 10, 2007 What is your question? You said "However, im having problems with radio buttions, and probably checkbox's when i get to them."your new 1) is about database. So, create an SQL query to check. You already did that.2. ) Look up the radio buttons and apply CHECKED as applicable. Did you try your script yet? What is the problem with it? Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted January 10, 2007 Share Posted January 10, 2007 The above script will never select the correct value asyou have checked= hardcoded.Now I am one for the standards so checked="anything" should not make the browser check the radio button.... not tried it but I bet it does....here is what you need [code]<?php$query = "SELECT sex FROM members WHERE user_name='$username'";$result = mysql_query($query) or die ("could not find sex");$row = mysql_fetch_assoc($result);if (strcmp($row['gender'], 'male') == 0){ $mcheck = ' checked="checked"'; $fcheck = NULL;}else{ $mcheck = NULL; $fcheck = ' checked="checked"';}?><label><input name="info_sex" type="radio" value="male" <?php echo $mcheck; ?>/>Male</label><label><input name="info_sex" type="radio" value="female"<?php echo $fcheck; ?> />Female</label>[/code]There is something wrong with your logic there.Repeating a query is a waste of resources.$row = mysql_fetch_array($result);$row['sex'] = $gender;That code seems to completly wipe out the value you have just queried - $gender is not set before this piont!I really don't want to offend you Jesie but you don't half look like Heather Brooke... sorry but you do ;) Quote Link to comment Share on other sites More sharing options...
Accurax Posted January 10, 2007 Author Share Posted January 10, 2007 the problem seems to be that it doesnt apply the checked status to the correct button, but rather to whatever the last one in the list might be.Let me try and clarify the problem some more,Lets say i have a choice of three radio buttons, where someone can tell me their favorite music1= rock2= classic3= SkaNow, the person visits the music tatse page, and sees a form allowing them to click on of these buttons...... so they choose classic at this point.the next day the person realises that they actually really like rock music, and they want to chenge their profileso they go back to the same page and select rock insteadThe second time they go to the page I want the form to have pre-selected the option that they entered last time they visited, so that they dont have to remember what they chose last time.The above php / sql script is an attempt to do this Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted January 10, 2007 Share Posted January 10, 2007 You only need to query the database once,[code]<?php// Assign username from session variable$username = $_SESSION['username'];// Query the database$query = "SELECT sex FROM members WHERE user_name = '$username'";$result = mysql_query($query) or die ("could not find sex");$row = mysql_fetch_array($result);// Stick the gender in a variable$gender = row['sex'];// Decide whether we're checked or not$male = if($gender == "male") ? 'checked="checked"' : '';$female = if($gender == "female") ? 'checked="checked"' : '';// Echo the form fieldsecho <<<HTML<input name="info_sex" type="radio" value="female" $female><strong>A Female</strong><br /><input name="info_sex" type="radio" value="male" $male><strong>A Male</strong><br />HTML;?>[/code]RegardsHuggie Quote Link to comment Share on other sites More sharing options...
Accurax Posted January 10, 2007 Author Share Posted January 10, 2007 [quote author=ToonMariner link=topic=121787.msg501406#msg501406 date=1168447212]The above script will never select the correct value asyou have checked= hardcoded.Now I am one for the standards so checked="anything" should not make the browser check the radio button.... not tried it but I bet it does....here is what you need [code]<?php$query = "SELECT sex FROM members WHERE user_name='$username'";$result = mysql_query($query) or die ("could not find sex");$row = mysql_fetch_assoc($result);if (strcmp($row['gender'], 'male') == 0){ $mcheck = ' checked="checked"'; $fcheck = NULL;}else{ $mcheck = NULL; $fcheck = ' checked="checked"';}?><label><input name="info_sex" type="radio" value="male" <?php echo $mcheck; ?>/>Male</label><label><input name="info_sex" type="radio" value="female"<?php echo $fcheck; ?> />Female</label>[/code]There is something wrong with your logic there.Repeating a query is a waste of resources.$row = mysql_fetch_array($result);$row['sex'] = $gender;That code seems to completly wipe out the value you have just queried - $gender is not set before this piont!I really don't want to offend you Jesie but you don't half look like Heather Brooke... sorry but you do ;)[/quote]Toon your a diamond.... thanks that script works perfectly... ive got the entire form working just the way i want now.And yes... Jesie is far to cute to be loitering around these forums :P 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.