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

    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
    Sydney, Australia
    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:
    "red"$checkred "checked"; break;
    "yellow"$checkyellow "checked"; break;
    "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
    (Note that the above is the equivalent of
    print (or echo) "<input type='radio' name='colour' value='red' $checkred >Red

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

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

  5. #5
    Join Date
    Oct 2006
    Ables Springs, Texas
    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
    Sydney, Australia
    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
    Ables Springs, Texas
    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
    can you post the insert page?that is the part i have a hard time with...

  9. #9
    Join Date
    Oct 2006
    Ables Springs, Texas
    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 "<center><table><tr><td>";
    echo "<h2>Record Added</h2>";
    echo "</td></tr></table></center>";

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


  11. #11
    Join Date
    Aug 2003
    Sydney, Australia
    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/>

    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.

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

  12. #12
    Join Date
    Feb 2010
    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
    Sydney, Australia
    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);
    $types mysql_fetch_array($result))
    $tname[$t] = $types['typename'];
    $tcode[$t] = $types['typecode'];

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

    print "
    Oh Lord, please help me be the person my dog thinks I am.

  14. #14
    Join Date
    Dec 2010

    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;
    <span class="bodytext">For Sale</span>
    		  <input type="radio" name="status" value="0" <?php if ($status!=1){ echo "checked=\"checked\""; } ?> >
    		<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.=") VALUES ('";
    please tel me a solution, is there any mistakes in the coding?

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

    Your query therefore reads
    PHP Code:
    VALUES ('
    $PROJECT_ID', 'mysql_real_escape_string($PROJECT_TITLE)', 'mysql_real_escape_string($PROJECT_STATUS)','')"
    It should be
    PHP Code:
    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 1 users browsing this thread. (0 members and 1 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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.17693 seconds
  • Memory Usage 3,033KB
  • Queries Executed 15 (?)
More Information
Template Usage (38):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (1)bbcode_html
  • (6)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (15)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (15)postbit
  • (15)postbit_onlinestatus
  • (15)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (74):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates