Jump to content

Recommended Posts

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]
Link to comment
https://forums.phpfreaks.com/topic/33599-radio-buttons/
Share on other sites

In simple terms this is what i want to do

1) check to see whether the database contains the string male or the string female

2) check the corresponding box

The 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
Link to comment
https://forums.phpfreaks.com/topic/33599-radio-buttons/#findComment-157421
Share on other sites

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?
Link to comment
https://forums.phpfreaks.com/topic/33599-radio-buttons/#findComment-157422
Share on other sites

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 ;)
Link to comment
https://forums.phpfreaks.com/topic/33599-radio-buttons/#findComment-157426
Share on other sites

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 music

1= rock
2= classic
3= Ska

Now, 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 profile

so they go back to the same page and select rock instead

The 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
Link to comment
https://forums.phpfreaks.com/topic/33599-radio-buttons/#findComment-157428
Share on other sites

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 fields
echo <<<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]

Regards
Huggie
Link to comment
https://forums.phpfreaks.com/topic/33599-radio-buttons/#findComment-157431
Share on other sites

[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
Link to comment
https://forums.phpfreaks.com/topic/33599-radio-buttons/#findComment-157484
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.