www.webdeveloper.com
Results 1 to 13 of 13

Thread: radio buttons in php? help!

  1. #1
    Join Date
    Mar 2003
    Posts
    59

    radio buttons in php? help!

    I have 2 simple forms, how can i pass radio button data (into a hidden field) from one form to the other using php?

    below is how I am achieving it with text fields but php deals with radio buttons differently apparently?

    PHP Code:

    <form action="mortgages3_s.php" method="post" name="mm">

    <input type="hidden" name="singleFullNametwo" value="<?php_echo_$_POST['singleFullName'];?>">
    <input type="hidden" name="singleDateofBirthtwo" value="<?php_echo_$_POST['singleDateofBirth'];?>">
    <input type="text" name="hair">

    <input type="submit">
    thanks

  2. #2
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    Yep, you can reference radio buttons the same way, as can be seen in this simple test:

    PHP Code:
    <?PHP

    if (isset($_POST["submit"])) {
        echo 
    $_POST["myradio"];
    }

    ?>

    <html>
    <head>
    </head>
    <body>
    <form action="<?PHP echo $_SERVER["PHP_SELF"]; ?>" method="post">
    One <input type="radio" name="myradio" value="one">
    Two <input type="radio" name="myradio" value="two">
    <input type="submit" name="submit" value="Submit">
    </form>
    </body>
    </html>

  3. #3
    Join Date
    Mar 2003
    Posts
    59
    what would PHP pass if I submitted this?

    PHP Code:

    <form_action="page2.php"_method="post"_name="radioform">
    <
    input_type="radio"_name="radiogroup"_value="yes"><input_type="radio"_name="radiogroup"_value="no">

    ________</form
    thanks

  4. #4
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    Depending on which one is checked, it would return "yes" or "no" when you access it like this on page2.php:

    $_POST["radiogroup"];

    This is also assumbing you change the understrikes to spaces

  5. #5
    Join Date
    Jul 2006
    Posts
    1
    What if you have more than two options that you want to be able to be selected, i have at least 4 choices that i need in one radio button set, how would that be done?

  6. #6
    Join Date
    Jun 2006
    Posts
    472
    Quote Originally Posted by Incendion
    What if you have more than two options that you want to be able to be selected, i have at least 4 choices that i need in one radio button set, how would that be done?
    you will have to use check boxes, radio buttons are only two values, yes/no, true/false, on/off, m/f understand?

  7. #7
    Join Date
    Jul 2006
    Posts
    38
    What's the deal with all the " _ "/s Doesn't that mess up your code?

  8. #8
    Join Date
    Jun 2006
    Posts
    472
    Quote Originally Posted by RogueDogg
    What's the deal with all the " _ "/s Doesn't that mess up your code?
    I's not code..it's just a statement to say that radio buttons are only for two values.. either yes or no etc.. If you need to have more than one option checked use check boxes or lists

  9. #9
    Join Date
    Jul 2006
    Posts
    38
    Quote Originally Posted by aussie girl
    I's not code..it's just a statement to say that radio buttons are only for two values.. either yes or no etc.. If you need to have more than one option checked use check boxes or lists
    Huh?

    <form_action="page2.php"_method="post"_name="radioform">
    <input_type="radio"_name="radiogroup"_value="yes"><input_type="radio"_name="radiogroup"_value="no" >

    ________</form>
    This above with the _ 's is ok? I wouldn't think it would be?

  10. #10
    Join Date
    Jun 2006
    Posts
    472
    Sorry RogueDogg I thought you were talking about my message and Pyro has already answered that question about the under scores above

  11. #11
    Join Date
    Feb 2010
    Location
    in the Darkness
    Posts
    17
    So, would you just use two different sets of radio button input tags, if you were using four radio button, and just check to see which one is set? And then carry out whatever action you need/want done from there?

    My teacher wants us to create a form where users enters 2 numbers and then clicks one of four radio buttons (add/subtract/multiply/divide). Then when they click the submit button, the user is transferred to the results page where their two numbers are either added, subtracted, multiplied, or divided depending on which radio button. All I'm getting is a blank page for the results, which is why I ask.

  12. #12
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    Use 1 group of 4 radio buttons - this makes the user choose only one of the 4, that's why radio buttons exist compared to checkboxes which is one or more. You could also use select (without the "multiple" attribute) but the radio button is more intuitive.

    Syntax and usage documented here.


    As to errors, post your code (the form and the script that processes the results) and we'll help you debug.

  13. #13
    Join Date
    Feb 2010
    Location
    in the Darkness
    Posts
    17
    Alright, here is the code I have for the time being for the two pages:

    Form Page:
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>
    2910_22.php
    </title>
    </head>
    <body>
    <!--author:shane nelson-->
    <form method="post" action="2910_22_get.php" name="form1">
    Enter #1: <input type="text" name="num1" /> <br />
    Enter #2: <input type="text" name="num2" /> <br />
    <input type="radio" name="add" value="add" />Addition <br /> <!--purpose: add-->
    <input type="radio" name="add" value="sub" />Subtraction <br /> <!--purpose: sub-->
    <input type="radio" name="add" value="mult" />Multiplication <br /> <!--purpose:mult-->
    <input type="radio" name="add" value="div" />Division <br /> <!--purpose:div-->
    <input type="submit" value="submit" />
    </form>
    </body>
    </html>

    Results Page:
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>
    2910_22_get.php
    </title>
    </head>
    <body>
    <!--author:shane nelson-->
    <?php
    $num1=$_POST['num1'];
    $num2=$_POST['num2'];
    $radio=$_POST['add'];
    if ($radio == add)
    {
    $y=$num1 + $num2;
    echo $num1." + ".$num2." = ".$y;
    }
    elseif ($radio == sub)
    {
    $y=$num1 - $num2;
    echo $num1." - "$num2." = ".$y;
    }
    elseif ($radio == mult)
    {
    $y=$num1 * $num2;
    echo $num1." X ".$num2." = ".$y;
    }
    else
    {
    $y=$num1 / $num2;
    echo $num1." / ".$num2." = ".$y;
    }
    ?>
    </body>
    </html>

    That's what I had last week, I haven't worked on this particular assignment recently. The form page works fine, but no matter what you put into the fields, you receive a blank page at the results page. This, I assume, has something to do with the radio buttons not working right in the code in the results page. Any help would be greatly appreciated.

    Oh, here's a link to the form page, if you want to see what it does when you try to input and submit:

    http://ito.auburncc.org/localuser/sn...10/2910_22.php

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



Recent Articles