Jump to content


Photo

MySql & Radio Buttons


  • Please log in to reply
7 replies to this topic

#1 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 02 September 2006 - 05:39 AM

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?

#2 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 02 September 2006 - 06:00 AM

wow... maybe it could use ajax... anyone?

#3 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 02 September 2006 - 06:05 AM

Or you could just pull it from the database thru mysql queries and echo it into the appriate radio button or what ever...
Why doesn't anyone ever say hi, hey, or whad up world?

#4 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 02 September 2006 - 06:24 AM

can u give me and example of what that may look like? this is what i am currently using (ghetto status):
<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">


#5 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 02 September 2006 - 06:25 AM

but i want a radio button instead...

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 02 September 2006 - 11:32 AM

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:
<?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>';
}?>

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

Ken

   

#7 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 02 September 2006 - 11:52 AM

$yes_sel = ($['showgeneral'] == 'y')?'checked':'';
needs ot be

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

just noticed the syntax colors were diffrent :)

Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#8 pro_se

pro_se
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts

Posted 02 September 2006 - 08:15 PM

it does not display correctly...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users