SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    validate select, check box and radio buttons

    Hi,

    I want to check if they are checked or selected before processing further.

    How can I possibly do that?

  2. #2
    SitePoint Addict wibble wobble's Avatar
    Join Date
    Dec 2008
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'll need to be a *lot* more specific...
    Find freelance jobs from all the major sites in one place:
    on twitter / on the web / twitter rss feed

  3. #3
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, just modified original post.

  4. #4
    SitePoint Addict wibble wobble's Avatar
    Join Date
    Dec 2008
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A quick test I did shows check-boxes and radio buttons are only submitted when at least one is active:

    PHP Code:
    <!--
     // http://www.sitepoint.com/forums/showthread.php?p=4069665
    -->
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      </head>
      <body>
        <form action="checkbox-selects-validation.php" method="POST">
          <input type="checkbox" name="cb[a]" value="ON" /> Checkboxes<br />
          <input type="checkbox" name="cb[b]" value="ON" /> Rock<br />
          <input type="checkbox" name="cb[c]" value="ON" /> Socks
          <br /><br />

          <select name="dd">
            <option>Drop</option>
            <option>downs</option>
            <option>smell</option>
          </select>
          <br /><br />

          <input type="radio" name="radio-ga-ga" value="1" />Rawr<br />
          <input type="radio" name="radio-ga-ga" value="2" />"Wake me up..."<br />
          <input type="radio" name="radio-ga-ga" value="3" />"...before you go-go"
          <br /><br />

          <input type="submit" value="Submit me!! CLick mE!! PleASE!!!" />
        </form>
        <?php

        print_r
    ($_POST);

        
    /*
         * When drop-down is left alone, the last 2 check-boxes are clicked,
         * and radios left empty:

           Array (
            [cb] => Array
                (
                    [b] => ON
                    [c] => ON
                )

            [dd] => Drop
           )
         */

        
    ?>
      </body>
    </html>
    Find freelance jobs from all the major sites in one place:
    on twitter / on the web / twitter rss feed

  5. #5
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, it's easy, radion buttons and checkboxes work the same way as other input fields. Just check that the field is set and isn't empty:

    PHP Code:
    if (isset($_POST['radio']) && !empty($_POST['radio']))
    {
        
    // some radio button was checked when submitting

    Just take into consideration that users can check more checkboxes and submit the form, so if you are expecting only one checked field, use radio buttons.

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To clarify, If a checkbox or radio button is not checked(either by the user, or by you setting a default one thats checked when the form renders) then the browser will not send any name/value pairs for it. Ths is in contrast to most other html form elements, where the browser will always send the name of the field even if the user hasn't filled in any value or made any selection from the menu.

    Just using a simple isset() works well for checkboxes(assuming they all have unique names), since theres only two possibilities, checked or unchecked, which corresponse to isset or !isset. Radio buttons need you to check the value as well since a radio group must all have the same name.

  7. #7
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by risoknop View Post
    Well, it's easy, radion buttons and checkboxes work the same way as other input fields. Just check that the field is set and isn't empty:

    PHP Code:
    if (isset($_POST['radio']) && !empty($_POST['radio']))
    {
        
    // some radio button was checked when submitting

    Just take into consideration that users can check more checkboxes and submit the form, so if you are expecting only one checked field, use radio buttons.
    It just not validates, the form process thoroughly without choosing an option

    I don't know why, but this is my select

    Code PHP:
    <?php
    						$Hchoices = array('Select end time', ' 1:00 am', '2:00 am', '3:00 am','4:00 am','5:00 am', '6:00 am', '7:00 am', '8:00 am', '9:00 am', '10:00 am', '11:00 am', '12:00 am', '1:00 pm', '2:00 pm', '3:00 pm', '4:00 pm', '5:00 pm', '6:00 pm', '7:00 pm', '8:00 pm', '9:00 pm', '10:00 pm', '11:00 pm', '12:00 pm');
    						echo "<select name='hour'>\n";
    						foreach($Hchoices as $Hkey=>$Hchoice)
    						{
    							echo "<option value='{$Hkey}'>{$Hchoice}</option>\n";
    						}
    						echo "</select>\n";
    						?>

  8. #8
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simply:
    PHP Code:
    if(isset($_POST['hour']) && !empty($_POST['hour'])){
     
    // do ur work

    Doesn't this work?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  9. #9
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works ok now, because of the approach I have to make the code like this. The && won't work in this case.

    Code PHP:
    $error_msg ="";
     
    if(!isset($_POST['hour']) || empty($_POST['hour']))
    {
    $error_msg = " Please make a select. ";
    }
     
    if($error_msg =="") { go ahead }

  10. #10
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Location
    USA
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    On a similar topic, I use some simple code along with a js file to make an htlm kg->pound converter.

    The code allows kg->pound to be preselected by default.

    The line of code that controls it is this:

    HTML Code:
    <input type="radio" value="0" name="choice" checked />
    When I try to validate it this one line of code throws and error due to the word "checked".

    The error is:

    Column 53: the name and VI delimiter can be omitted from an attribute specification only if SHORTTAG YES is specified .
    <input type="radio" value="0" name="choice" checked /></td>

    "VI delimiter" is a technical term for the equal sign. This error message means that the name of an attribute and the equal sign cannot be omitted when specifying an attribute. A common cause for this error message is the use of "Attribute Minimization" in document types where it is not allowed, in XHTML for instance.

    How to fix: For attributes such as compact, checked or selected, do not write e.g <option selected ... but rather <option selected="selected" ...


    I found the suggestion by W3C a bit confusing, moreover I cannot find the solution to fix this online, so I came here.

    Any suggestions?

    Thank you in advance.

  11. #11
    SitePoint Evangelist bals28mjk's Avatar
    Join Date
    Aug 2007
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try giving the checked attribute a value of "checked" like this:
    Code:
    <input type="radio" value="0" name="choice" checked="checked" />
    Or change the SHORTTAG to YES as instructed.

  12. #12
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Location
    USA
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bals28mjk View Post
    Try giving the checked attribute a value of "checked" like this:
    Code:
    <input type="radio" value="0" name="choice" checked="checked" />
    Or change the SHORTTAG to YES as instructed.
    Thank you! Truth is, I don't think that works, because in xhtml all attributes must be quoted, Soooooooooo

    I tried this approach:

    checked="checked" and it worked!


    Thank you.

  13. #13
    SitePoint Evangelist bals28mjk's Avatar
    Join Date
    Aug 2007
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem 4evrblu, I'm glad you got it.

    If you are looking to write valid xhtml, you can always use a validation service to help ensure that.


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
  •