Jump to content

validating checkbox !


ilikephp

Recommended Posts

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'";} ?> />

Link to comment
https://forums.phpfreaks.com/topic/157830-validating-checkbox/
Share on other sites

<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.

Link to comment
https://forums.phpfreaks.com/topic/157830-validating-checkbox/#findComment-832468
Share on other sites

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...

 

Link to comment
https://forums.phpfreaks.com/topic/157830-validating-checkbox/#findComment-832470
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/157830-validating-checkbox/#findComment-832478
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.