473,498 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

validate checkbox

63 New Member
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";
  13.  
  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>
  36.  
  37. </body>
  38. </html>
  39.  
thanks
May 13 '09 #1
5 7167
Dormilich
8,658 Recognized Expert Moderator Expert
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
lolodede
63 New Member
it doesnt check if the checkbox are checked or not
May 13 '09 #3
Dormilich
8,658 Recognized Expert Moderator Expert
used your code for a test and it worked.
May 13 '09 #4
lolodede
63 New Member
yes its working thanks alot wat about how can i tell user to enter value into textbox when they select other
thanks
May 13 '09 #5
Dormilich
8,658 Recognized Expert Moderator Expert
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

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

Similar topics

0
1984
by: Kumar | last post by:
Hi , I am using the article code shown in below url: http://www.codeproject.com/aspnet/xmlxsltransformer.asp?df=100&forumid=118504&select=1170328 This article is very cool feature. I...
2
3455
by: NishSF | last post by:
Would anyone have any suggestions/javascript code so that if one clicks the Radio Button "Yes" below he has the option of selecting any of the six CheckBox below. If the user clicks on Radio Button...
1
359
by: jayparaiso | last post by:
Hi! How to validate check box and drop down menu in one form?
5
1878
by: timothy.pollard | last post by:
Hi I'm having a bit of bother trying to make a questionnaire do what I want it to. I have put it up on www.web-iq.co.uk/test.htm. Basically the user of the final form (when I've tarted it up)...
1
19110
by: Scott | last post by:
I'm trying to validate a checkbox below on Sumbit. If the checkbox named "myForm.myCheckBox" isn't checked, then the code in CODE 1 displays False, which is correct. However, when I test that...
2
3264
by: Zvonko Bi¹kup | last post by:
Hi! I have 5 checkboxes with the same name: <input type="checkbox" name="check" value="XYZ" /> <input type="checkbox" name="check" value="ZXY" /> <input type="checkbox" name="check" value="MHG"...
1
1852
by: anilareddy | last post by:
I have an application like this. I need to validate the start time in 2nd row against the start row in first row. I mean The start time value entered in the second row must not b the value entered in...
1
2822
by: mbarnhizer | last post by:
Hello All, Trying to figure out how to validate a series of questions on an online test. I am thinking that VB or Javascript is the best route, but your input may make a difference. The site i...
4
4142
by: CF FAN | last post by:
I want to validate this two checkbox variables on an action page and also an update page <cfinput type="checkbox" name="gender"checked> </label></td> <td>Female</td> ...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.