Link to home
Start Free TrialLog in
Avatar of bcmeyer1983
bcmeyer1983

asked on

PHP verify checkbox is checked

OK still new to PHP. But i am developing the start page of a website that checks to make sure a checkbox is checked before proceeding to the next page. any ideas on how to make this happen. here is my code so far:

HTML:
<html>

<head>
<title>Wesbite Name</title>
</head>

<body>
<form>
<form action="agree.php" method=POST>
<p align="left"><input type="checkbox" name="agree" value="ON">
By checking this you agree</p>
<p align="left"><input type=submit value="Continue" name="submit"></p>
<p></p>
</form>
</body>

</html>


PHP:

$agree=$_POST['agree']

if
Avatar of Joe Wu
Joe Wu
Flag of Australia image

<?php
if(!isset($_POST['agree']))
{
    // code to go back, agree not checked!
    exit();
}
// do your stuff here, agree checked!
?>

hope this helps.
This is actually better handled by javascript in an onSubmit handler.

However, if you want to check is it is checked in php:

if (!$_REQUEST["agree"] == "ON") {
  echo "You did not check the agree checkbox";
} else {
  Go on...
}
Avatar of bcmeyer1983
bcmeyer1983

ASKER

help me out here:

<?php
$link="http://www.website.com";

if(!isset($_POST['agree']));
{
header("location:$link");
}
else
echo please agree to terms

?>
something like this maybe:

<?php
$link="http://www.website.com";
if(!isset($_POST['agree']));
{
echo "Please agree to the terms, it is all I am asking!"
}
else
{
header("location:$link");
}
?>
Yes, your second one looks ok. I forgot about using isset but I would recommend using the $_REQUEST variable
if you are using PHP5.
You were very close:

<?php
$link="http://www.website.com";
if(!isset($_POST['agree']))
{
      echo "Please agree to the terms, it is all I am asking!";
}
else
{
header("Location: $link");
}
?>
EddieShipman,

Interesting recommendation, however I was debating with someone over this (and i proposed what you said there) and he (my friend) made a good point in saying that you should know where the variables are coming from, either POST or GET, by using REQUEST you are exposing your code to either methods, taking this as an example if you use $_REQUEST then you can pass in a url like:

http://mydomain.com/something.php?agree=ON

and the if condition would validate, however this would not work if $_POST was used.

Well, I suppose so, but I tend to test my scripts using get. I never thought about it like that.
ok here is what ive update so far guys with what you have provided me (agree.php):

<?php
$link="http://www.website.com";
if(!isset($_POST['agree'])) {
echo "Please agree to the terms, it is all I am asking!";
}
else {
header("location:$link");
}
?>

<html>

<head>
<title>Check here to agree</title>
</head>

<body>

<form action="agree.php" method=POST>
      <p><input type="checkbox" name="agree" value="ON">Check here to agree
      <input type=submit value="Login" style="float: left"></p>
      &nbsp;</form>

</body>

</html>


the only issue that i have is that i dont want the echo to show up until after the post. will this require seperate html and php files or can i do it in the same file like i currently have.
This will fix it


<?php
$link="http://www.website.com";
if(!isset($_POST['agree']) && isset($_POST["is_submit"]) && $_POST["is_submit"]==1) {
echo "Please agree to the terms, it is all I am asking!";
}
else {
header("location:$link");
}
?>

<html>

<head>
<title>Check here to agree</title>
</head>

<body>

<form action="agree.php" method=POST>
      <p><input type="checkbox" name="agree" value="ON">Check here to agree
      <input type=submit value="Login" style="float: left">
      <input type="hidden" name="is_submit" value="1">
      </p>
      &nbsp;</form>

</body>

</html>
my error:

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\agree.php:2) in C:\wamp\www\agree.php on line 8
ASKER CERTIFIED SOLUTION
Avatar of Joe Wu
Joe Wu
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sweet, makes sense now that i see it all. learned a new one. thanks for sharing the expertise!
Kudos
no problem glad to help :)