Using checkboxes as a means of confirmation.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Using checkboxes as a means of confirmation.

Post by social_experiment »

If I added a checkbox to a form asking a visitor to check it before the form can be submitted, could this be fooled somehow and the form submitted without the checkbox checked?

Code: Select all

 
<html>
 Check tick box before submitting form
 <form method="post" action="processPage.php">
      <input type="checkbox" name="checkBoxToCheck" value="Y" />
      <input type="submit" value="Submit" />
 </form>
</html>
 

I would use isset() to check if the checkbox had been set and I would also check the value of the checkbox and from that determine whether the code should execute.

Code: Select all

 
<?php
  if ( isset($_POST['checkBoxToCheck']) || ($_POST['checkBoxToCheck'] == '' ) {
     // code to execute
  }
  else {
     // alternate code
  }
?>
 
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
iamngk
Forum Commoner
Posts: 50
Joined: Mon Jun 29, 2009 2:20 am

Re: Using checkboxes as a means of confirmation.

Post by iamngk »

you have to change if condition.

Code: Select all

<?php
  if ( isset($_POST['checkBoxToCheck']) && $_POST['checkBoxToCheck'] == 'Y' ) {
     // code to execute
  }
  else {
     // alternate code
  }
?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Using checkboxes as a means of confirmation.

Post by social_experiment »

Ok, so that both conditions are met at the same time.

Thanks :)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply