By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 974 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

validate checkbox

P: 63
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
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $name = 0;
  3. $cusID=0;
  4. $hobbies =0 ;
  5. if (isset ($_POST["customername"])) {
  6. if($_POST["customername"] == NULL)
  7. $name=1;
  8. if($_POST ["customerid"] == NULL)
  9. $cusID=1;
  10. }
  11. if(!isset($_POST["hobbies"])) {
  12.    echo "Please check the box";
  14. ?> 
  15. <h1>Hobby Selection Page<br /></h1>
  16. <form method="post" action="task14.php" id="hobbies" >
  17.    <p>
  18.        <label for="customername">Customer Name: </label><input type="text" id="customername" name="customername"/><?php if ($name) echo "please enter customer name"?>
  19. <br/>
  20.            <label for="customerid">Customer ID Number: </label><input type="text" id="customerid" name="customerid"/><br/>
  21.     <?php if($cusID) echo "please enter value in customer id"?>
  22.        </p>
  23.     <p>Please select between 1 and 3 hobbies from the list below:<br/>
  24.       <input type="checkbox" name="hobbies[]" id="cbx1" value="stamp" /><label for="cbx1">Stamp Collecting</label> <br/>
  25.       <input type="checkbox" name="hobbies[]" id="cbx2" value="movies" /><label for="cbx2">Movies</label> <br/>
  26.       <input type="checkbox" name="hobbies[]" id="cbx3" value="golf" /><label for="cbx3">Golf</label> <br/>
  27.       <input type="checkbox" name="hobbies[]" id="cbx4" value="bush" /><label for="cbx4">Bush Walking</label> <br/>
  28.       <input type="checkbox" name="hobbies[]" id="cbx5" value="read" /><label for="cbx5">Reading</label> <br/>
  29.       <input type="checkbox" name="hobbies[]" id="cbx6" value="music" /><label for="cbx6">Music</label> <br/>
  30.       <input type="checkbox" name="hobbies[]" id="cbx7" value="other" /><label for="cbx7">Other</label> &nbsp;<input type="text" name="other" /><br/>
  31.         <br/>
  32.     <input type="submit" value="Submit Form"/>
  33.     <input type="reset" value="Clear Form" />
  34.   </p>
  35.     </form>
  37. </body>
  38. </html>
May 13 '09 #1
Share this Question
Share on Google+
5 Replies

Expert Mod 5K+
P: 8,639
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.
May 13 '09 #2

P: 63
it doesnt check if the checkbox are checked or not
May 13 '09 #3

Expert Mod 5K+
P: 8,639
used your code for a test and it worked.
May 13 '09 #4

P: 63
yes its working thanks alot wat about how can i tell user to enter value into textbox when they select other
May 13 '09 #5

Expert Mod 5K+
P: 8,639
something like
Expand|Select|Wrap|Line Numbers
  1. if (in_array('other', $_POST['hobbies']) and !$_POST['other'])
  2. if (in_array('other', $_POST['hobbies']) xor $_POST['other']) // should also work
May 13 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.