Ok this is my first post so please be gentle...

I have tried several ways to get data to a mysql database from radio buttons that have male or female options..

I had it saving to the database but couldn't get the data out and into the radio button...
example : gender is M ...

Now I can get the 'M' to echo from the database and display on the page but can't get the radio button to be set to checked...

// code for form details -- this goes to a bit of javascript ...

<?php
if(!isset($_SESSION))
{
session_start();
}
$musoID = $_SESSION['musoID'];
$username = $_SESSION['username'];

echo "You are logged in as $username. ";

require ('../mdb/connect.php'); //require ('../mrequire/getMyDetails.php');

$sqlGetDetails = "SELECT * FROM muso WHERE username = '$username'";
$result = mysql_query($sqlGetDetails);

while($row = mysql_fetch_array($result)) {
   $rid = $row['musoID'];
   $rusername = $row['username'];
   $rfname = $row['fName'];
   $rlname = $row['lName'];
   $remail = $row['email'];
   $rphone = $row['phone'];
   $rgender = $row['gender'];
   $rage = $row['age'];
   $raboutMe = $row['aboutMe'];//action=\"mrequire/updateDetails.php\"
}

         echo <<<_END
         <fieldset>
         <legend>My Personal Details</legend>
            <form method="POST">
            <div id="ajaxresponse">
            </div>
            First Name:<br />
            <input type="text" id="txtFname" name="txtFname" value="$rfname"/><br />
            Last Name:<br />
            <input type="text" id="txtLname" name="txtLname" value="$rlname"/><br />
            Email:<br />
            <input type="text" name="txtEmail" id="txtEmail" value="$remail"/><br />
            Phone:<br />
            <input type="text" name="txtPhone" id="txtPhone" value="$rphone"/><br />
            Gender:<br />
_END;
if($rgender == "F"){
   echo "girl!<br />
   <input type=\"radio\" name=\"rbGender\" value=\"M\" /> Male
   <input type=\"radio\" name=\"rbGender\" value=\"F\" checked=\"checked\" />Female<br />";
}
elseif($rgender == "M") {
   echo "not girl<br />
   <input type=\"radio\" id=\"rbGender\" name=\"rbGender\" value=\"M\" checked=\"checked\" /> Male
   <input type=\"radio\" id=\"rbGender\" name=\"rbGender\" value=\"F\" >Female<br />";
}
else {
   echo "<input type=\"radio\" name=\"rbGender\" value=\"M\" /> Male
         <input type=\"radio\" name=\"rbGender\" value=\"F\" checked=\"checked\" />Female<br />";
}
echo <<<_END
            Age:<br />
            <input type="text" id="txtAge" name="txtAge" value="$rage" /><br />
            About Me:<br />
            <textarea rows="4" cols="16" id="txtAboutMe" name="txtAboutMe">$raboutMe</textarea><br />
            <input type="button" id="btnSaveDetails" onclick="SaveDetails();" value="Save Details" />
            </form>
         </fieldset>
_END;

?>

here's the js file

function SaveDetails()
{
   $.ajax({
      type: "POST",
      url: "mrequire/updateDetails.php",
      data: "txtFname=" + $('#txtFname').val() + "&txtLname=" + $('#txtLname').val() +
      "&txtEmail=" + $('#txtEmail').val() + "&txtPhone=" + $('#txtPhone').val() +
      "&rbGender=" + $("input[name='rbGender']").val() + "&txtAge=" + $('#txtAge').val() +
      "&txtAboutMe=" + $('#txtAboutMe').val(),
      success: function(html){
         $('#ajaxresponse').html(html);
      }
   });
}

here's the update details file--

<?php
session_start();
require ('../mdb/connect.php');
require ('../msql/mysqlFixStrings.php');
$PROMPT = "";
$username = $_SESSION['username'];
echo "<h4>Details for $username have been updated!</h4>";
$fname = mysql_fix_string($_POST['txtFname']);      //echo "your first name is $fname<br />";
$lname = mysql_fix_string($_POST['txtLname']);      //echo "your last name is $lname<br />";
$email = mysql_fix_string($_POST['txtEmail']);      //echo "your email is $email<br />";
$phone = mysql_fix_string($_POST['txtPhone']);      //echo "your phone number is $phone<br />";
$age = mysql_fix_string($_POST['txtAge']);          //echo "your age is $age<br />";
$gender = mysql_fix_string($_POST['rbGender']);     //echo "you are a $gender<br />";
$aboutMe = mysql_fix_string($_POST['txtAboutMe']);  //echo "this is about you $aboutMe";

$fname = strip_tags($fname);
$lname = strip_tags($lname);
$email = strip_tags($email);
$phone = strip_tags($phone);
$age = strip_tags($age);
$gender = strip_tags($gender);
$aboutMe = strip_tags($aboutMe);


echo $gender;

$queryUpdate = "update muso set fName = '$fname', lName = '$lname', email = '$email', phone = '$phone',
                age = '$age', gender = '$gender', aboutMe = '$aboutMe' where username = '$username'";//// need session musoID!!!

$result = mysql_query($queryUpdate);

?>

All of the code except the radio buttons works...

Any help is greatly appreciated.... by the way I'm pretty new to php and programming in general... so sorry if I ask dumb stuff...

Cheers

Recommended Answers

All 3 Replies

assuming you have:

<form id="theForm"...>

on your SECOND block of code you posted, in line 6 - instead of: data: ...monstrosity of statements your currently have here... simply use: data: $('#theForm').serialize() The serialize() method should be able to determine accurately which of your radio buttons is actually checked. The problem you are having is that you are NOT limiting your selection to the checked item:

$("input[name='rbGender']:checked").val()

assuming you have:

<form id="theForm"...>

on your SECOND block of code you posted, in line 6 - instead of: data: ...monstrosity of statements your currently have here... simply use: data: $('#theForm').serialize() The serialize() method should be able to determine accurately which of your radio buttons is actually checked. The problem you are having is that you are NOT limiting your selection to the checked item:

$("input[name='rbGender']:checked").val()

Cheers hielo !

Wow I wasn't expecting such a quick response!

To be honest I wasn't sure about the serialize() bit.... but I did try using the

$("input[name='rbGender']:checked").val()

and it seems to be working now.....

I think I'll go with that before my fragile little mind explodes!

Thanks again

Cheers hielo !

Wow I wasn't expecting such a quick response!

To be honest I wasn't sure about the serialize() bit.... but I did try using the

$("input[name='rbGender']:checked").val()

and it seems to be working now.....

I think I'll go with that before my fragile little mind explodes!

Thanks again

Oh the serialize works a treat! So much easier!

Many thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.