Jump to content

Archived

This topic is now archived and is closed to further replies.

pro_se

MySql & Radio Buttons

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?

Share this post


Link to post
Share on other sites
Or you could just pull it from the database thru mysql queries and echo it into the appriate radio button or what ever...

Share this post


Link to post
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]

Share this post


Link to post
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

   

Share this post


Link to post
Share on other sites
[code]$yes_sel = ($['showgeneral'] == 'y')?'checked':'';[/code]
needs ot be

[code]$yes_sel = ($r['showgeneral'] == 'y')?'checked':'';[/code]

just noticed the syntax colors were diffrent :)

Liam

Share this post


Link to post
Share on other sites

×

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.