SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to validate radio button in PHP

    Can anyone explain how I can validate this radio button PHP script.

    PHP Code:
    <?

    <script language='javascript'>
      function 
    show_errors(msg)
      {
        if (
    document.getElementById("errors").value != "")
        {
          
    msgdocument.getElementById("errors").value;
          
    alert(msg);
        }
    //end if
      
    }
      

      
    </script>


        <table width="400" border="0">
          <tr>
            <td><input type="radio" name="q<?php echo $ID?>" value="SA">
            SA</td>
            <td><input type="radio" name="q<?php echo $ID?>" value="A">
            A</td>
            <td><input type="radio" name="q<?php echo $ID?>" value="N">
            N </td>
            <td><input type="radio" name="q<?php echo $ID?>" value="D">
            D</td>
            <td><input type="radio" name="q<?php echo $ID?>" value="SD">
            SD</td>
            <td><input type="radio" name="q<?php echo $ID?>" value="N/A">
            NA </td>
          </tr>
        </table>
        
        
        <?php
    How can I validate the radio buttons that the users will be selecting. Right now if the user did not select anything, the script get submitted with empty answer in the database.

    But I want the asnwers to be validated and checked before they submit it and should not allow submission without selecting any option.

    Can anyone help pls.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If you want the check before they submit as you said, you should use JavaScript validation.

    If you want to check after they submit but before you insert into the database, the isset() function should do the trick.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    radio button

    Can you give a sample , I will apply it to my work.

    Thanks.

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by david4u
    Can you give a sample , I will apply it to my work.

    Thanks.
    i *highly* recommend that you use a forms class. Manuel Lemos' forms class is excellent and can be found at phpclasses.org.

    there are others, too.

    i get client side (javascript) and server side (php) validation. i get regex validation. javascript points the cursor to the first input box. click on my label and the element is highlighted. i get linked selects. he has some ajax there that i haven't played with. there are tons of goodies in his class. here's a simple snippet...

    PHP Code:
    $form->NAME="enter_customer";
    $form->METHOD="POST";
    $form->ACTION=$PHP_SELF;
    $form->ID="content_form";

    $form->debug="OutputDebug";

    // add form inputs
    $form->AddInput(array(
        
    "TYPE"=>"text",
        
    "NAME"=>"customer_name",
        
    "ID"=>"customer_name",
        
    "CLASS"=>"input",
        
    "SIZE"=>20,
        
    "MAXLENGTH"=>60,
        
    "ValidateAsNotEmpty"=>1,  "ValidateAsNotEmptyErrorMessage"=>"you must enter a customer",
        
    "LABEL"=>"Customer Name",
        
    "TABINDEX"=>1,
    )); 

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    radio button validation

    Thanks alot.Your sample helps.

    I have solved it now.


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
  •