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...
PHP Code:
switch($colour)
{
case "red": $checkred = "checked"; break;
case "yellow": $checkyellow = "checked"; break;
case "blue": $checkblue = "checked"; break;
}
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";
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.
print "<form... >";
for($x=1;$x<=$t;$x++)
{
$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/>";
}
print "</form>";
Oh Lord, please help me be the person my dog thinks I am.
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".
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.
Oh Lord, please help me be the person my dog thinks I am.
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'];
Bookmarks