www.webdeveloper.com
Results 1 to 6 of 6

Thread: validating radio buttons in PHP

  1. #1
    Join Date
    Feb 2005
    Location
    Sydney, Australia
    Posts
    103

    validating radio buttons in PHP

    I am trying to validate two radio buttons - if neither is checked an error message is given:

    PHP:

    Code:
    $selected_radio = $_POST['rad_unittype'];
    
    if ($selected_radio == 'myunit') {
    	$myunit_status = 'checked';
    	}
    else if ($selected_radio == 'common') {
    	$common_status = 'checked';
    	}
    if(($myunit_status != 'checked') && ($common_status != 'checked')) {
    	$("#rad_unittype1").after('<br><label class="error" id="rad_unittype1_error">Please select the Job Location</label>');
    	return false
    	}
    HTML:

    Code:
    <div>
         <fieldset>
         <legend>This job is located here:</legend>
         <input value="myunit" id="rad_unittype_1" name="rad_unittype" type="radio" /> <label for="rad_unittype_1">at my unit</label>
         <input value="common" id="rad_unittype_2" name="rad_unittype" type="radio" /> <label for="rad_unittype_2">common to all units</label>
         </fieldset>
    </div>
    It doesn't work and I can't see why. I think it's the PHP part.
    Last edited by tpeck; 06-06-2012 at 05:24 AM.

  2. #2
    Join Date
    Apr 2012
    Posts
    22
    It looks fine. Maybe you have forgotton to wrap your inputs in <form> tag or have another input with the same name?

    Paste all the html here.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,251
    It's often useful to just find out what the value is, so you know what sort of bugs to look for:
    PHP Code:
    <?php
    echo "<pre>".print_r($_POSTtrue)."</pre>";
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Feb 2005
    Location
    Sydney, Australia
    Posts
    103
    Thanks.

    The complete html is too long, but the page source is viewable at the link below.

    It all works well if I remove the radio buttons completely. It actually sends the info about the radio button if one is selected, but if neither is selected, it bypasses the captcha and submits instead of flagging the error.

    The testbed demo page is here:

    http://aapress.com.au/PRD/forms/maintenance.php

    I added the test code to process2.php as well.
    Last edited by tpeck; 06-06-2012 at 12:54 PM.

  5. #5
    Join Date
    Feb 2005
    Location
    Sydney, Australia
    Posts
    103
    I changed the PHP to this:

    Code:
    // job location check
    			
    $myunit_status = 'unchecked';
    $common_status = 'unchecked';
    			
    $selected_radio = $_POST['rad_unittype'];
    
    if ($selected_radio == 'myunit') {
        $myunit_status = 'checked';
    }
    else if ($selected_radio == 'common') {
       $common_status = 'checked';
    }
    else if(($myunit_status == 'unchecked') && ($common_status == 'unchecked')) {
       $("#rad_unittype1_error").remove();
       $("#rad_unittype1").after('<br><label class="error" id="rad_unittype1_error">Please select the Job Location</label>');
       return false
    }
    else
       {
       $("#rad_unittype1_error").html('');
       }
    ...but it just refuses to show the error if neither radio button is selected - and always submits, bypassing the last data entry field and captcha. Grrr.

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,251
    Quote Originally Posted by tpeck View Post
    I changed the PHP to this:

    Code:
    // job location check
    			
    $myunit_status = 'unchecked';
    $common_status = 'unchecked';
    			
    $selected_radio = $_POST['rad_unittype'];
    
    if ($selected_radio == 'myunit') {
        $myunit_status = 'checked';
    }
    else if ($selected_radio == 'common') {
       $common_status = 'checked';
    }
    else if(($myunit_status == 'unchecked') && ($common_status == 'unchecked')) {
       $("#rad_unittype1_error").remove();
       $("#rad_unittype1").after('<br><label class="error" id="rad_unittype1_error">Please select the Job Location</label>');
       return false
    }
    else
       {
       $("#rad_unittype1_error").html('');
       }
    ...but it just refuses to show the error if neither radio button is selected - and always submits, bypassing the last data entry field and captcha. Grrr.
    Looks like you have some JavaScript (JQuery?) in the middle of the second elseif()???
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

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