validate checkbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lolodede
    New Member
    • Apr 2009
    • 63

    validate checkbox

    i would like to make sure user at least one checkbox and if it select otherand need to make sure to write something in text box
    Code:
    <?php
    $name = 0;
    $cusID=0;
    $hobbies =0 ;
    if (isset ($_POST["customername"])) {
    if($_POST["customername"] == NULL)
    $name=1;
    if($_POST ["customerid"] == NULL)
    $cusID=1;
    }
    if(!isset($_POST["hobbies"])) {
       echo "Please check the box";
    } 
    
    ?> 
    <h1>Hobby Selection Page<br /></h1>
    <form method="post" action="task14.php" id="hobbies" >
       <p>
       	<label for="customername">Customer Name: </label><input type="text" id="customername" name="customername"/><?php if ($name) echo "please enter customer name"?>
    <br/>
    	   	<label for="customerid">Customer ID Number: </label><input type="text" id="customerid" name="customerid"/><br/>
    	<?php if($cusID) echo "please enter value in customer id"?>
       	</p>
    	<p>Please select between 1 and 3 hobbies from the list below:<br/>
          <input type="checkbox" name="hobbies[]" id="cbx1" value="stamp" /><label for="cbx1">Stamp Collecting</label> <br/>
          <input type="checkbox" name="hobbies[]" id="cbx2" value="movies" /><label for="cbx2">Movies</label> <br/>
          <input type="checkbox" name="hobbies[]" id="cbx3" value="golf" /><label for="cbx3">Golf</label> <br/>
          <input type="checkbox" name="hobbies[]" id="cbx4" value="bush" /><label for="cbx4">Bush Walking</label> <br/>
          <input type="checkbox" name="hobbies[]" id="cbx5" value="read" /><label for="cbx5">Reading</label> <br/>
          <input type="checkbox" name="hobbies[]" id="cbx6" value="music" /><label for="cbx6">Music</label> <br/>
          <input type="checkbox" name="hobbies[]" id="cbx7" value="other" /><label for="cbx7">Other</label> &nbsp;<input type="text" name="other" /><br/>
    		<br/>
        <input type="submit" value="Submit Form"/>
        <input type="reset" value="Clear Form" />
      </p>
        </form>
    
    </body>
    </html>
    thanks
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I don't see the problem with the code. you're testing for the values… so you just need to specify what to do if the requirements are not met.

    Comment

    • lolodede
      New Member
      • Apr 2009
      • 63

      #3
      it doesnt check if the checkbox are checked or not

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        used your code for a test and it worked.

        Comment

        • lolodede
          New Member
          • Apr 2009
          • 63

          #5
          yes its working thanks alot wat about how can i tell user to enter value into textbox when they select other
          thanks

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            something like
            Code:
            if (in_array('other', $_POST['hobbies']) and !$_POST['other'])
            if (in_array('other', $_POST['hobbies']) xor $_POST['other']) // should also work

            Comment

            Working...