SitePoint Sponsor

User Tag List

Results 1 to 25 of 25

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Jul 2010
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Code for radio button

    Good day!

    I never used radio button. Now I need to add radio button in my webpage. Radio button 1 is mother and radio button 2 is child. Is it in my database it is only 1 field or separate field one for mother and one for child?

    Kindly give me a code for radio button.

    Thank you...

  2. #2
    SitePoint Member
    Join Date
    Nov 2010
    Location
    Philippines
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a quick example.
    Fire up your HTML editor and try this:

    Code HTML4Strict:
    <form action="">
    <input type="radio" name="sex" value="male" /> Male<br />
    <input type="radio" name="sex" value="female" /> Female
    </form>

    From here you can use PHP to make this more
    dynamic.

    Such as getting the categories for the radio
    button from the database.

    Did this help?

  3. #3
    SitePoint Addict
    Join Date
    Jul 2010
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mattlebrun View Post
    Here's a quick example.
    Fire up your HTML editor and try this:

    Code HTML4Strict:
    <form action="">
    <input type="radio" name="sex" value="male" /> Male<br />
    <input type="radio" name="sex" value="female" /> Female
    </form>

    From here you can use PHP to make this more
    dynamic.

    Such as getting the categories for the radio
    button from the database.

    Did this help?
    Is it only one field in the database?

    Thank you

  4. #4
    SitePoint Member
    Join Date
    Nov 2010
    Location
    Philippines
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rhodarose View Post
    Is it only one field in the database?

    Thank you
    Not necessarily so.

    Let's say you have a DB table containing records that will be
    used as radio button category.

    Simply loop through the search result of that and add the
    selection.

    Let's say you've place the results in an array so the code
    would look something like this:
    Code PHP:
    $radioNum = 0;
     
    foreach ($searchResult as $radioSelection) {
     
        $radioNum++;
     
        echo '<input type="radio" name="radio' . $radioNum . '" value="'. $radioSelection .'" />' . $radioSelection;
     
    }

    I know this is very rough but I'm just trying to
    communicate the idea.

    Plus I'm just stealing time here in the office

    Could someone give another example here?

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,600
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by mattlebrun View Post
    foreach ($searchResult as $radioSelection) {

    $radioNum++;

    echo '<input type="radio" name="radio' . $radioNum . '" value="'. $radioSelection .'" />' . $radioSelection;
    }
    Doing that would make each radio button a separate group of one and therefore invalid since you must have at least two radio buttons per group. The code you have there is more appropriate for checkboxes where each checkbox is a separate field.

    With radio buttons you have one field per group of buttons and each button just supplies a different value.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Addict
    Join Date
    Jul 2010
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mattlebrun View Post
    Not necessarily so.

    Let's say you have a DB table containing records that will be
    used as radio button category.

    Simply loop through the search result of that and add the
    selection.

    Let's say you've place the results in an array so the code
    would look something like this:
    Code PHP:
    $radioNum = 0;
     
    foreach ($searchResult as $radioSelection) {
     
        $radioNum++;
     
        echo '<input type="radio" name="radio' . $radioNum . '" value="'. $radioSelection .'" />' . $radioSelection;
     
    }

    I know this is very rough but I'm just trying to
    communicate the idea.

    Plus I'm just stealing time here in the office :D

    Could someone give another example here?
    I want to happen is if the user choose mother the mother was save in the database

  7. #7
    SitePoint Member
    Join Date
    Oct 2010
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rhodarose View Post
    I want to happen is if the user choose mother the mother was save in the database
    i give the answer you'd better try it

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,600
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by rhodarose View Post
    I want to happen is if the user choose mother the mother was save in the database
    Each user's form should have its own record in the database.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  9. #9
    SitePoint Addict svcghost's Avatar
    Join Date
    Oct 2010
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well.. If there are two conditions here, one being mother, the other being child, then it would be logical to use ONE field (e.g. value = 0 = mother, value = 1 = child).

    Here is some simple code for a form with two radio buttons in it:
    Code:
    <form action="name_of_php_file_with_insert_code.php" method="post" id="formid" name="formname">
    <input type="radio" name="mother" id="mother" value="Mother" /> Mother<br />
    <input type="radio" name="child" id="child" value="Child" /> Child
    </form>

  10. #10
    SitePoint Addict
    Join Date
    Jul 2010
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by svcghost View Post
    Well.. If there are two conditions here, one being mother, the other being child, then it would be logical to use ONE field (e.g. value = 0 = mother, value = 1 = child).

    Here is some simple code for a form with two radio buttons in it:
    Code:
    <form action="name_of_php_file_with_insert_code.php" method="post" id="formid" name="formname">
    <input type="radio" name="mother" id="mother" value="Mother" /> Mother<br />
    <input type="radio" name="child" id="child" value="Child" /> Child
    </form>
    I used it but how can I make sure that if the user choose the mother it would save in the database?and if the mother is already choose the child would not be clickable

  11. #11
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,600
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by rhodarose View Post
    I used it but how can I make sure that if the user choose the mother it would save in the database?and if the mother is already choose the child would not be clickable
    All of the radio buttons in a group are clickable EXCEPT for the one already selected. Clicking on any of the others will change which one is selected.

    With a radio button group there is only one field regardless of whether there are two buttons or two thousand buttons. The buttons just select what value to put in the field.

    So with those two radio buttons and mother already selected the only clickable button is child. If it is clicked then it will be selected and not clickable and mother will be unselected and clickable.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  12. #12
    SitePoint Addict
    Join Date
    Jul 2010
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    All of the radio buttons in a group are clickable EXCEPT for the one already selected. Clicking on any of the others will change which one is selected.

    With a radio button group there is only one field regardless of whether there are two buttons or two thousand buttons. The buttons just select what value to put in the field.

    So with those two radio buttons and mother already selected the only clickable button is child. If it is clicked then it will be selected and not clickable and mother will be unselected and clickable.
    but in my situation, when the mother was click and click the child they are both click

  13. #13
    SitePoint Member
    Join Date
    Nov 2010
    Location
    Philippines
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rhodarose View Post
    but in my situation, when the mother was click and click the child they are both click
    This shouldn't happen normally.

    It would help if you could show us the code.

  14. #14
    SitePoint Addict
    Join Date
    Jul 2010
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What should be the field type for this radio button in the database?

    Thank you

  15. #15
    SitePoint Member
    Join Date
    Oct 2010
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <form action="name_of_php_file_with_insert_code.php" method="post" id="formid" name="formname">
    <input type="radio" name="person" id="mother" value="Mother" /> Mother<br />
    <input type="radio" name="person" id="child" value="Child" /> Child
    </form>
    then the sql is
    insert into 'tablename'('person')values("$_POST['person']")

  16. #16
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    You're trying to save $_POST["pack_setting"], but the radio buttons have the name 'pack'.
    They should be the same.

    And you really should escape the posted data before sending it to MySQL.


Bookmarks

Posting Permissions

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