SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  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 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>

  4. #4
    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

  5. #5
    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

  6. #6
    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']")

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 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="^$">

  8. #8
    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?

  9. #9
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 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="^$">

  10. #10
    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

  11. #11
    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

  12. #12
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 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="^$">

  13. #13
    SitePoint Addict
    Join Date
    Jul 2010
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Each user's form should have its own record in the database.
    Is it theres no code for the saving the choose radio button in the database?

    Thank you

  14. #14
    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 theres no code for the saving the choose radio button in the database?

    Thank you
    Best thing to do here is to test it right.

    Check if you can get the right values upon
    form submission.

    Once the user submits the form get the value
    of the radio button that was selected and
    insert that to the database.

    I can't give an example but that's the idea.

    Anyone mind giving a concrete example?

  15. #15
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by rhodarose View Post
    Is it theres no code for the saving the choose radio button in the database?

    Thank you
    Once the form is submitted the radio button group is no different from an ordinary input field and can be processed the same way. The only difference is that the only values you expect the field to have are those of the buttons in the group.
    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="^$">

  16. #16
    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

  17. #17
    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.

  18. #18
    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

  19. #19
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by rhodarose View Post
    What should be the field type for this radio button in the database?

    Thank you
    That is dependent on what values you use with the buttons. If you use numbers as the values then you can use a integer field. If you use text then char or varchar.
    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="^$">

  20. #20
    SitePoint Addict
    Join Date
    Jul 2010
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I used this code:
    PHP Code:
    include 'con.php';
    $query "INSERT INTO transact (pack_setting) VALUES ('" $_POST["pack_setting"] . "')";

    echo 
    "<input type='radio' name='pack' id='mother' value='Mother'  /> Mother Lot<br />";
    echo 
    "<input type='radio' name='pack' id='child' value='Child' /> Child Lot"
    But when i choose Mother radio button it did not save to the database

  21. #21
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by rhodarose View Post
    I used this code:
    PHP Code:
    include 'con.php';
    $query "INSERT INTO transact (pack_setting) VALUES ('" $_POST["pack_setting"] . "')";

    echo 
    "<input type='radio' name='pack' id='mother' value='Mother'  /> Mother Lot<br />";
    echo 
    "<input type='radio' name='pack' id='child' value='Child' /> Child Lot"
    But when i choose Mother radio button it did not save to the database
    Where's your form tag in the HTML and what's the server side code in the page the form calls that takes the form content and loads it into 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="^$">

  22. #22
    SitePoint Addict
    Join Date
    Jul 2010
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Where's your form tag in the HTML and what's the server side code in the page the form calls that takes the form content and loads it into the database?
    Only the data for pack was not save the rest that i input was saved.

    Thank you

  23. #23
    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.

  24. #24
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by rhodarose View Post
    Only the data for pack was not save the rest that i input was saved.

    Thank you
    You haven't shown enough of the code to see what is happening - the selected radio button will be in $_POST['pack'] after the form is submitted. The only field your code shows is $_POST["pack_setting"] and you don't show the form field that comes from.
    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="^$">

  25. #25
    SitePoint Addict
    Join Date
    Jul 2010
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    You haven't shown enough of the code to see what is happening - the selected radio button will be in $_POST['pack'] after the form is submitted. The only field your code shows is $_POST["pack_setting"] and you don't show the form field that comes from.
    I change the $_POST["pack_setting"] to $_POST["pack"]

    Thank you...it works


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
  •