Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Radio Button Form

    Hi there
    Say you have a form with 3 options as follows:

    1. Apples ()
    2. Oranges ()
    3. Pears ()

    and you want the user to check one of the three options. For example, if apples is checked off, and then they check oranges, apples unchecks automatically and oranges appears checked off. In HTML this is a Radio button list...

    How would you set up the table in the database to accept the option chosen?

    Would it be ENUM ('Yes','No') and then ask PHP to put YES on the chosen field in the DB and NO in the 2 others?

    Thanks

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,623
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Surely one field in the database where you insert a value based on the option chosen is the easiest way. After all that's what you get passed from the form with the radio buttons.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Oh I see... So I don't have to set up 3 frields? I just call the field "fruit" and then pass on the chosen fruit into that field?

    Any help on how that would look like in MySQL codE?

    Thanks

  • #4
    Regular Coder
    Join Date
    Dec 2005
    Posts
    346
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I use the ENUM Data Type & the code below. What ever you have set for the Value for the each button is the data that is entered into the db.
    This is the Radio buttons
    Code:
    <input name="fruit_types" type="radio" value="Apples">
    <input name="fruit_types" type="radio" value="Oranges">
    <input name="fruit_types" type="radio" value="Pears">
    This is the php code to insert into the db
    PHP Code:
       $fruit_type $_POST['fruit_types'];

        
    $sql mysql_query("INSERT INTO table (fruit) VALUES('$fruit_type')")
            or die (
    mysql_error()); 

  • #5
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks Lansing
    I'll use that to code the page... I meant though, in the table, in my database, how do I create that field? Is it just ENUM with no values and wait until the form passes the value?

  • #6
    Regular Coder
    Join Date
    Dec 2005
    Posts
    346
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ENUM('Apples','Oranges','Pears')

    When you go to set up the table. Just enter the following into the Length/Values field & you must include the single quotes & comma's:
    PHP Code:
    'Apples','Oranges','Pears' 

  • #7
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks for the help! Let me give this a whirl and see how it works.

    I appreciate your time spent on this.


  •  

    Posting Permissions

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