Hi I am trying to create a, check-box for a user registration form. When user fills all fields, he/she will be given a check box to confirm, that says " I agree with the terms and conditions, But, before registering, I need PHP to check the value whether the checkbox was submitted or not. This is the HTML <form method="POST" action="testing.php" > I agree with the terms <input type="checkbox" name="chbox" value="1" /><br/> <input type="submit" name="check" /> Code (markup): And this is the PHP <?php if(isset($_POST['check'])){ $result = $_POST['chbox']; echo $result; } Code (markup): Now, this script will echo out, 1 if the checkbox is checked or Undefined index... if not. I need to know, if I am doing something wrong, because I don't want the error to appear if the checkbox is not checked. I can suppress the error by adding, @ before the variable, but I don't think that is a good codding practice.
if (isset($_POST['check'])): if (isset($_POST['chbox'])): echo 'You need to select an option'; else: $result = $_POST['chbox']; echo $result; endif; endif; PHP:
Thanks but if I click submit, while the checkbox is unchecked if gives me this error. Notice: Undefined index: chbox in C:\xampp\htdocs\DIRECTORY\TESTING\testing.php on line -- Refering this this code $result = $_POST['chbox']; Code (markup):
Because the code @ThePHPMaster gave you was wrong way round. You can either do !isset for the second if or change the statements.
<?php if (isset($_POST['check'])) { $result = false; if (isset($_POST['chbox'])) { $result = $_POST['chbox']; } echo $result; } ?> Code (markup):
Thanks guys. Specially hassanahmad2 I twicked the code like this if (isset($_POST['check'])): if (!isset($_POST['chbox'])): echo '0'; else: $result = $_POST['chbox']; echo $result; endif; endif; ?> Code (markup): Hope it is secure.
This is secure and I cut out what is not needed: Note that empty() checks if it is set and that it is null.