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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I validate radio buttons in PHP?

    Ok, so I have one document here called test.php and I want to validate my radio buttons so that when the user is filling out the form and clickes submit but didn't pick one of the radio buttons, then the data gets sent back to the original form and an error message appears beside the form.

    Here's my first document "test.php"

    PHP Code:
    <?php
    @$semester=$_GET["semester"];
     @
    $semestererror=$_GET["semestererror"];

     
    ?>
    <html>

    <h1>Entry Form</h1> <!--My title-->
    </html>
    <?php
    if(isset($semester)==true)
    $semestererror="*Please select a semester";
    else
    $semestererror="&nbsp";

    ?>
    <!--My form-->
    <form name="Stumarks" action="test2.php" method="GET"> 
    <p><font color="red"><?php echo $semestererror?></font></p>Semester: <br />
     <input type="radio" name="semester" value="fall"  />Fall<br /> <?php if ($semester="fall") echo "checked="true""?><!--Radio Buttons-->
     <input type="radio" name="semester" value="spring" />Spring<br /> <?php if ($semester="spring") echo "checked="true""?>
    <input type="submit" value="submit"/>
     </form>
    And here's my second document "test2.php"

    PHP Code:
    <?php 
    @$semester=$_GET["semester"];
     @
    $semestererror=$_GET["semestererror"];

    if (
    $semester="" or $semestererror=0)
    {
    header ("location: test.php?semester=$semester&&semestererror=$semestererror");}

    if (
    $semester="fall" or $semester="spring" or $semestererror=1)

    echo 
    "<table border=1>";
    echo 
    "<th> <h2>".semester."</h2> </th>
    "
    ;
     
    echo 
    "</table>";  

    ?>
    I just don't know what i did wrong?

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    First of all, in an "if" statement use two equal signs for compare, example:

    if ($semester=="fall" or $semester=="spring" or $semestererror==1)

    That's as far as I got ... gotta leave the house for a while.

  • #3
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    First of all, in an "if" statement use two equal signs for compare, example:

    if ($semester=="fall" or $semester=="spring" or $semestererror==1)

    That's as far as I got ... gotta leave the house for a while.
    I did that, and it didn't make a difference!

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    repost it again ... with your changes.

    repost both scripts ... because there were errors in the other as well.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    You should be using isset($_GET["semester"]) to determine if a radio button was selected. If no button was selected then the variable will not exist.
    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.


  •  

    Posting Permissions

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