Jump to content

MySql & Radio Buttons


pro_se

Recommended Posts

Hey All! I am having a problem with using radio buttons to change an integer in a mysql database... The values they need to have are 'y' and 'n'. And when someone opens a page it gets the information from the database and it sets the default selection for the radio button as what is in the database (y or n). any suggestions?
Link to comment
Share on other sites

can u give me and example of what that may look like? this is what i am currently using (ghetto status): [code]<input name="showgeneral" type="text" id="showgeneral" value="<?php
$username = $_SESSION['username'];
$result = mysql_query("SELECT * FROM users WHERE username='$username'");
while($r=mysql_fetch_array($result))
{
  $showgeneral=$r["showgeneral"];
 
echo "$showgeneral";
}
?>" size="2" maxlength="1"> [/code]
Link to comment
Share on other sites

You need to get the value from the database before you create the input statements, so you can determine which button is presented as being checked:
[code]<?php
$username = $_SESSION['username'];
$result = mysql_query("SELECT * FROM users WHERE username='$username'");
while($r=mysql_fetch_assoc($result)) {
    $no_sel = ($r['showgeneral'] == 'n')?'checked':'';
    $yes_sel = ($['showgeneral'] == 'y')?'checked':'';
    echo 'Yes: <input name="showgeneral" type="radio" id="showgeneral_yes" value="y" ' . $yes_sel . '><br>';
    echo 'No: <input name="showgeneral" type="radio" id="showgeneral_no" value="n" ' . $no_sel . '><br>';
}?>[/code]

Note: This code hasn't been checked for syntax errors...

Ken

   
Link to comment
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.