www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24

Thread: HTML Form, Radio Buttons and MYSQL Value

  1. #1
    Join Date
    Aug 2005
    Posts
    5

    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.

  2. #2
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    700
    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;

    and in your html form, something like this...
    HTML Code:
    ?>
    <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
    <?php
    (Note that the above is the equivalent of
    print (or echo) "<input type='radio' name='colour' value='red' $checkred >Red
    etc)

    Hope that helps.
    CTB
    Oh Lord, please help me be the person my dog thinks I am.

  3. #3
    Join Date
    Aug 2005
    Posts
    5
    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.

  4. #4
    Join Date
    May 2007
    Location
    Denver
    Posts
    19
    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

    PHP Code:
    ?>

    <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
    <?php

  5. #5
    Join Date
    Oct 2006
    Location
    Ables Springs, Texas
    Posts
    139
    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.

  6. #6
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    700
    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.
    Oh Lord, please help me be the person my dog thinks I am.

  7. #7
    Join Date
    Oct 2006
    Location
    Ables Springs, Texas
    Posts
    139
    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'];

  8. #8
    Join Date
    Aug 2006
    Posts
    7
    can you post the insert page?that is the part i have a hard time with...

  9. #9
    Join Date
    Oct 2006
    Location
    Ables Springs, Texas
    Posts
    139
    Sure thing, if it will help you out. But, everything has been working flawlessly on this end since my last post.

    Code:
    <?php
    include("dbinfo.inc.php");
    
    $code=$_POST['code'];
    $progname=$_POST['progname'];
    $progurl=$_POST['progurl'];
    $id=$_POST['id'];
    $joinbonus=$_POST['joinbonus'];
    $reflevels=$_POST['reflevels'];
    $earnratio=$_POST['earnratio'];
    $surftimer=$_POST['surftimer'];
    $allowurls=$_POST['allowurls'];
    $script=$_POST['script'];
    $owner=$_POST['owner'];
    $email=$_POST['email'];
    $reason=$_POST['reason'];
    $terms=$_POST['terms'];
    $vout=$_POST['vout'];
    
    $query = "INSERT INTO programs VALUES (
    	'$code',
      '$progname',
      '$progurl',
      '$id',
      '$joinbonus',
      '$reflevels',
      '$earnratio',
      '$surftimer',
      '$allowurls',
      '$script',
      '$owner',
      '$email',
      '$reason',
      '$terms',
      '$vout'
    )";
    mysql_query($query)or die("Unable to insert data" . mysql_error());
    echo "<center><table><tr><td>";
    echo "<h2>Record Added</h2>";
    echo "</td></tr></table></center>";
    mysql_close();
    ?>

  10. #10
    Join Date
    Feb 2010
    Posts
    2
    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.

    cheers

  11. #11
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    700
    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;

    <form.... >
    <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/>
    </form>

    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.

    Cheers
    CTB
    Oh Lord, please help me be the person my dog thinks I am.

  12. #12
    Join Date
    Feb 2010
    Posts
    2
    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?

  13. #13
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    700
    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...
    PHP Code:
    $t 0;
    $query "SELECT * FROM industrytypes";
    $result mysql_query($query);
    while(
    $types mysql_fetch_array($result))
    {
       
    $t++;
       
    $tname[$t] = $types['typename'];
       
    $tcode[$t] = $types['typecode'];

    Then, instead of the above html, try this...
    PHP Code:
    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.

  14. #14
    Join Date
    Dec 2010
    Posts
    6

    radio button

    hi i need a hep to solve this;

    i have two radio buttons and i want to send their values (1 or 0) to the mysql database PROJECT_STATUS field, but the database will not updating, below are the codes

    html form code;
    Code:
    <span class="bodytext">For Sale</span>
    		  <input type="radio" name="status" value="0" <?php if ($status!=1){ echo "checked=\"checked\""; } ?> >
    		&nbsp;&nbsp;&nbsp;
    		<span class="bodytext">Completed</span>
    		<input type="radio" name="status" value="1" <?php if ($status==1){ echo "checked=\"checked\""; } ?> >
    
    php query sav code;
    
    $PROJECT_ID = GUID::newGUID();
    $PROJECT_TITLE = $_REQUEST['title'];
    $PROJECT_STATUS = $_REQUEST['status'];
    
    $queryStr="INSERT INTO project (";
    		$queryStr.="PROJECT_ID,";
                    $queryStr.="PROJECT_TITLE,";
                    $queryStr.="PROJECT_STATUS,";
    
    $queryStr.=") VALUES ('";
    		$queryStr.=$PROJECT_ID."','";
    		$queryStr.=mysql_real_escape_string($PROJECT_TITLE)."','";
    
    $queryStr.=mysql_real_escape_string($PROJECT_STATUS)."','";
    	$queryStr.="')";
    
    mysql_query($queryStr);
    	header('Location:'.'home.php');
    please tel me a solution, is there any mistakes in the coding?

  15. #15
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    700
    In your query string, you have a comma after PROJECT_STATUS.

    Your query therefore reads
    PHP Code:
    $query="INSERT INTO project (PROJECT_ID, PROJECT_TITLE, PROJECT_STATUS,) 
    VALUES ('
    $PROJECT_ID', 'mysql_real_escape_string($PROJECT_TITLE)', 'mysql_real_escape_string($PROJECT_STATUS)','')"
    It should be
    PHP Code:
    $query="INSERT INTO project (PROJECT_ID, PROJECT_TITLE, PROJECT_STATUS) 
    VALUES ('
    $PROJECT_ID','mysql_real_escape_string($PROJECT_TITLE)', 'mysql_real_escape_string($PROJECT_STATUS)')"
    Oh Lord, please help me be the person my dog thinks I am.

Thread Information

Users Browsing this Thread

There are currently 3 users browsing this thread. (0 members and 3 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles