ilikephp Posted May 12, 2009 Share Posted May 12, 2009 Hi, How can I validate a checkbox plz? I am using the script below and it gives me an error: in header: $accept = trim($_POST['accept']); if (empty($accept)) { $error['accept'] = '* required'; } In HTML: <input type="checkbox" name="accept" id="accept" <?php if(isset($error)) {echo "value='$accept'";} ?> /> Quote Link to comment Share on other sites More sharing options...
jackpf Posted May 12, 2009 Share Posted May 12, 2009 What's the error? Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted May 12, 2009 Share Posted May 12, 2009 Is it just me... or does your checkbox only have a value attribute if there is an error? Quote Link to comment Share on other sites More sharing options...
ilikephp Posted May 12, 2009 Author Share Posted May 12, 2009 the error is: Notice: Undefined index: accept in C:\www\x.php on line 34 the line 34 contains: $accept = trim($_POST['accept']); if (empty($accept)) { $error['accept'] = '* required'; } Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 I assume $_POST['accept'] doesn't exist. You should check if the form is submitted. Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted May 12, 2009 Share Posted May 12, 2009 Again, because your field only has a value if there is an error... the value in a checkbox should always be there. How else would you put data in it? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 Again, because your field only has a value if there is an error... the value in a checkbox should always be there. How else would you put data in it? It's a checkbox, you don't put data in it. And HTML errors are not PHP errors. Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted May 12, 2009 Share Posted May 12, 2009 <input type="checkbox" name="accept" id="accept" value='whatevervaluethecheckboxrepresents'/> Also, there shouldn't be any reason to use trim() on a checkbox... it only passed the contents of the value attribute so unless you put spaces in the value, it will not trim anything. Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted May 12, 2009 Share Posted May 12, 2009 Again, because your field only has a value if there is an error... the value in a checkbox should always be there. How else would you put data in it? It's a checkbox, you don't put data in it. And HTML errors are not PHP errors. Um... His checkbox has no value?? I am very confused as to your reply as he is trying to set the value with the contents of a variable... Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted May 12, 2009 Share Posted May 12, 2009 I see what you mean.. you just missed that I was saying exactly the same thing... The value attribute is set in the html code... you don't get to enter data, only a check. he is trying to enter data. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 That's fine and all, but HTML will just ignore it. It doesn't solve his error. Quote Link to comment Share on other sites More sharing options...
ilikephp Posted May 12, 2009 Author Share Posted May 12, 2009 when I tick the checkbox I dont get any error, if I click submit without checking the box I get the error: he is my whole code: <?php if (array_key_exists('ewComments', $_POST)) { // mail processing script // remove escape characters from POST array if (get_magic_quotes_gpc()) { function stripslashes_deep($value) { $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); return $value; } $_POST = array_map('stripslashes_deep', $_POST); } // validate the input, beginning with name $name = trim($_POST['name']); if (empty($name)) { $error['name'] = '* required'; } $email = $_POST['email']; // check for valid email address $pattern = '/^[^@]+@[^\s\r\n\'";,@%]+$/'; if (!preg_match($pattern, trim($email))) { $error['email'] = '* required'; } $password = trim($_POST['password']); $password2 = trim($_POST['password2']); if ($password != $password2){ $error['password'] = 'Wrong Password'; } if (empty($password)) { $error['password'] = '* required'; } $accept = trim($_POST['accept']); if (empty($accept)) { $error['accept'] = '* required'; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Registration Form</title> <?php include('styles/style_rules.php'); ?> <script type="text/JavaScript"> <!-- function MM_findObj(n, d) { //v4.01 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) { d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);} if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n]; for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); if(!x && d.getElementById) x=d.getElementById(n); return x; } function MM_validateForm() { //v4.0 if (document.getElementById){ var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments; for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]); if (val) { nm=val.name; if ((val=val.value)!="") { if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@'); if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n'; } else if (test!='R') { num = parseFloat(val); if (isNaN(val)) errors+='- '+nm+' must contain a number.\n'; if (test.indexOf('inRange') != -1) { p=test.indexOf(':'); min=test.substring(8,p); max=test.substring(p+1); if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n'; } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; } } if (errors) alert('The following error(s) occurred:\n'+errors); document.MM_returnValue = (errors == ''); } } //--> </script> </head> <body> <div id="wrapper"> <div id="maincontent"> <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" name="contactForm" id="contactForm"> <h1 align="center">Course Registration Form </h1> <table width="516" border="1" align="center" bgcolor="#cccccc"> <tr> <td width="179">Full Name:</td> <td width="321"><label for="name"></label> <span class="warning"> <input type="text" name="name" id="name" <?php if(isset($error)) {echo "value='$name'";} ?> /> </span><span class="warning"> <?php if (isset($error['name'])) { ?> <?php echo $error['name']; ?> <?php } ?> </span><br /></td> </tr> <tr> <td>Address</td> <td><label> <textarea name="textarea" id="textarea" cols="25" rows="4"></textarea> </label></td> </tr> <tr> <td>Email</td> <td><span class="warning"> <input type="text" name="email" id="email" <?php if(isset($error)) {echo "value='$email'";} ?> /> <?php if (isset($error['email'])) { ?> <?php echo $error['email']; ?> <?php } ?> </span></td> </tr> <tr> <td>Password</td> <td><label><span class="warning"> <input type="text" name="password" id="password" <?php if(isset($error)) {echo "value='$password'";} ?> /> <?php if (isset($error['password'])) { ?> <?php echo $error['password']; ?> <?php } ?> </span></label></td> </tr> <tr> <td>Confirm Password</td> <td><span class="warning"> <input type="text" name="password2" id="password2" <?php if(isset($error)) {echo "value='$password2'";} ?> /> <?php if (isset($error['password'])) { ?> <?php echo $error['password']; ?> <?php } ?> </span></td> </tr> <tr> <td>Date of Birth</td> <td><?php # Script 2.6 - calendar.php // This script makes three pull-down menus // for an HTML form: months, days, years. // Make the months array: $months = array (1 => 'January','February', 'March', 'April', 'May','June', 'July', 'August', 'September','October', 'November', 'December'); // Make the days and years arrays: $days = range (1, 31); // Make the months pull-down menu: echo '<select name="month">'; foreach ($months as $key => $value) { echo "<option value=\"$key\">$value </option>\n"; } echo '</select>'; // Make the days pull-down menu: echo '<select name="day">'; foreach ($days as $value) { echo "<option value=\"$value\">$value </option>\n"; } echo '</select>'; echo '</select>'; ?> <input name="year" type="text" id="year" onblur="MM_validateForm('year','','NinRange1975:2050');return document.MM_returnValue" size="4" maxlength="4" /> (yyyy)</td> </tr> <tr> <td>Gender</td> <td><table width="175" border="0"> <tr> <td><input name="radiobutton" type="radio" value="radiobutton" /> Male </td> <td><input name="radiobutton" type="radio" value="radiobutton" /> Female</td> </tr> </table></td> </tr> <tr> <td>Please choose topics of interests </td> <td><table width="223" border="0" align="left"> <tr> <td><input type="checkbox" name="checkbox" value="checkbox" /> VB.NET <br /></td> <td><input type="checkbox" name="checkbox2" value="checkbox" /> SQL</td> </tr> <tr> <td><input type="checkbox" name="checkbox3" value="checkbox" /> PHP </td> <td><input type="checkbox" name="checkbox4" value="checkbox" /> Java</td> </tr> </table></td> </tr> <tr> <td align="right"><p>Hobby</p> <p>(Select multiple choices by holding the ctrl key and clicking on them one by one)</p></td> <td align="center"><select name="select" size="7" multiple="multiple"> <option>Outdoor Sports</option> <option>Adventure Sports</option> <option>Pop Music</option> <option>Rock Music</option> <option>Aggressive Music</option> <option>Photography</option> </select></td> </tr> <tr> <td align="left"><p>I accept the </p> <p>Learning Contract</p></td> <td align="center"><span class="warning"> <input type="checkbox" name="accept" id="accept" value='whatevervaluethecheckboxrepresents'/> <?php if(isset($error)) {echo "value='$accept'";} ?> /> </span><span class="warning"> <?php if (isset($error['accept'])) { ?> <?php echo $error['accept']; ?> <?php } ?> </span></td> </tr> <tr> <td colspan="2" align="center"><input name="ewComments" type="submit" id="ewComments" value="Send comments" /></td> </tr> </table> <p> </p> <p> <label for="email"></label> <br /> </p> <p> </p> </form> </div> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 When you say you get an error, don't you think it would be relevant to post the error message rather than just saying you have an error? Quote Link to comment Share on other sites More sharing options...
ilikephp Posted May 12, 2009 Author Share Posted May 12, 2009 I post it before: Notice: Undefined index: accept in C:\www\x.php on line 34 Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 I assume $_POST['accept'] doesn't exist. You should check if the form is submitted. And I posted that before. ^ Quote Link to comment Share on other sites More sharing options...
jackpf Posted May 12, 2009 Share Posted May 12, 2009 Checkboxes aren't sent if they're not checked. So you'll have to use isset(), or turn off notices. Quote Link to comment Share on other sites More sharing options...
ilikephp Posted May 12, 2009 Author Share Posted May 12, 2009 Sry, but I got lost ??? How can I check if the form is submitted plz? Thanks in advance... Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 Check if $_POST['ewComments'] is set or not. :-\ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.