Re-populate Radio Buttons and Check Boxes

Posted on 2005-05-09
Last Modified: 2011-10-03
Hi Experts,
I use a web form to allow our customers to post Job Orders on our website. The web form uses text boxes, check boxes and radio buttons. I am going to write a CGI script that will allow users to go back and edit their existing Job Orders on the website.

My question is: I know how to re-populate the Text Boxes, but when it comes to Radio Buttons and Check Boxes, that is a whole new animal to me. Once I grab the values from my mySQL database, how do I re-populate the values for the Check Boxes and Radio Buttons? I have never tried to do this before.

Thanks everyone in advance for all of your help,

Question by:jahmekyat
    LVL 8

    Expert Comment

    Both elements, checkboxes and radios, have an additional html attribute: checked
    So you have to compare for every checkbox and every radio button wether that elements value was checked before and set the "checked" attribute if it was.
    LVL 25

    Accepted Solution

    see this example based on some hidden fields u populate radio and checkboxes

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <TITLE> New Document </TITLE>


    <form name = "frm">
    <input type = "hidden" name = "rad" value = "yes">
    <input type = "hidden" name = "chk" value = "true">

    <input type = "radio" name = "radio1" value = "yes">yes

    <input type = "radio" name = "radio1" value = "no">no

    <input type = "checkbox" name = "chk1" >

    for(i=0; i <document.frm.radio1.length;i++)
    if(document.frm.radio1[i].value == document.frm.rad.value)
    document.frm.radio1[i].checked = true;

    if(document.frm.chk.value == "true")
    document.frm.chk1.checked = true;

    LVL 17

    Expert Comment

    No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

    I will leave the following recommendation for this question in the Cleanup topic area:
        Split between NETTY4 and archrajan

    Any objections should be posted here in the next 4 days. After that time, the question will be closed.

    EE Cleanup Volunteer

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
    This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now