HTML Form, Radio Buttons and MYSQL Value
I need to know how to retrieve a value from a MySql database to a radio button on a HTML form. I have been able to retrieve the text field, just not the radio buttons.
The radio button is an HTML construct and has nothing to do, directly, with the value in your database. mySQL doesn't store 'radio buttons', just values. To 'translate' those values, you just need a bit of php to check the value you've extracted from the db, and then apply it to your html.
Here's a quick example...
Let's say you have three possible colours, 'red', 'yellow', or 'blue' that are selected by the user from your html form.
You've said you can already read the text, so let's assume you've retrieved the colour from your mySQL data and assigned the string value to $colour.
Your php just needs to assign a "checked" to the right radio button in your html form, a bit like this...
and in your html form, something like this...
case "red": $checkred = "checked"; break;
case "yellow": $checkyellow = "checked"; break;
case "blue": $checkblue = "checked"; break;
(Note that the above is the equivalent of
<input type="radio" name="colour" value="red" <?=$checkred?> >Red
<input type="radio" name="colour" value="yellow" <?=$checkyellow?> >Yellow
<input type="radio" name="colour" value="blue" <?=$checkblue?> >Blue
print (or echo) "<input type='radio' name='colour' value='red' $checkred >Red
Hope that helps.
I tried getting this but stumbled, care to give a more indepth detail regarding having say industry categories for a specif user and the radio button of the selected industry type selected by this user ends up being selected or "checked". thats what I am trying to archive.
I'm not sure what you mean by "more in-depth", but here's a quick overview...
In your mysql table 'companies', in a field called 'industry', you store industry group values in a field.
a = Accounting
b = Banking
c = Computers
d = Defence
In your php, you need to query the mysql, and extract those values, something like this;
$query = "SELECT * FROM companies WHERE record = '$therecord'";
$result = mysql_query($query);
Then you need to get your values like this;
$row = mysql_fetch_array($result); //assuming $therecord is a unique value (I use the autoincrement value)
Now you need to read the value and then set the radio buttons as 'checked' like this;
$type = $row[industry];
$checked[$type] = "checked";
FInally, your html looks like this;
<input type='radio' name='industry' value='a' $checked['a']>Accounting<br/>
<input type='radio' name='industry' value='b' $checked['b']>Bankng<br/>
<input type='radio' name='industry' value='c' $checked['c']>Computers<br/>
<input type='radio' name='industry' value='d' $checked['d']>Defence<br/>
Using the array $checked makes the coding exercise a little easier.
When this is submitted from the browser, the receiving script simply updates (or inserts) the value a, b, c, or d in the database.
I hope that helps.
I followed this through and not getting the result I want. I tried different queries that would yeild the industry type for a specific user but I would also like it to list the rest of the industry categories. The next way to do this is to have this first query list the industry type for this user, then have a separate query which yields all the industry categories which to me is a bit unorthodox.
Whats your take?
I would be putting the industry type in its own table.
That gives you the ability to add industry types as needed.
Then, add a read of the industry types...
Then, instead of the above html, try this...
$t = 0;
$query = "SELECT * FROM industrytypes";
$result = mysql_query($query);
while($types = mysql_fetch_array($result))
$tname[$t] = $types['typename'];
$tcode[$t] = $types['typecode'];
print "<form... >";
$c = $tcode[$x]; //i do this because it makes the code easier to read
print "<input type='radio' name='industry' value='".$c".' ".$checked[$c]>".$tname[$x]."<br/>";
thanks, that worked. I just could not find any where on the web where this was explained. Most examples when over text boxes and that was it.
The ternary operator is also really good for setting the checked flag. The ternary operator has form:
$x = (condition)? 'val1' : 'val2';
x will be val1 or val2 depending on the condition
<input type="radio" name="colour" value="red" <?=($colour=='red')? 'checked' : '' ?> >Red
<input type="radio" name="colour" value="yellow" <?=($colour=='yellow')? 'checked' : '' ?> >Yellow
<input type="radio" name="colour" value="blue" <?=($colour=='blue')? 'checked' : '' ?> >Blue
I have a form where I want to list either active programs, or suspended programs. That status is reflected in the "code" field of my database, as either "a" or "s".
Here is my form data:
<input type="radio" name="code" value="a">Active
<input type="radio" name="code" value="s">Suspended
And here is my query:
$query = "SELECT * FROM programs WHERE code='$code' ORDER BY progname ASC";
Problem is, regardless of which radio button I check, it returns all entries, both active and suspended.
I'd be checking two things...
First, check that your script is receiving the value code.
I usually use $_REQUEST['name'], because it doesn't matter whether it's submitted by GET or POST.
It woude be
$code = $_REQUEST['code'];
Note that I do recall reading somewhere in the php manual that one should avoid using a field name that's the same as the variable name, so try changing one or the other and maybe that will help.
The next thing I'd check is your actual query. It sounds obvious, but sometimes just getting your script to print the $query that was executed can help you track down a problem.
One thing I overlooked, which has been corrected now, is form reset. I have multiple search forms on one page, so adding a form reset button seems to have corrected the problem, at least for now.
And thanks for the tip about using $code = $_REQUEST['code'];
can you post the insert page?that is the part i have a hard time with...
Sure thing, if it will help you out. But, everything has been working flawlessly on this end since my last post.
$query = "INSERT INTO programs VALUES (
mysql_query($query)or die("Unable to insert data" . mysql_error());
echo "<h2>Record Added</h2>";
Is it inserting instead of updating, or just failing completely?
yeh it is inserting instead of updating
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)