Solved

PHP verify checkbox is checked

Posted on 2007-12-04
14
368 Views
Last Modified: 2013-12-13
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
0
Comment
Question by:bcmeyer1983
  • 6
  • 5
  • 3
14 Comments
 
LVL 21

Expert Comment

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

hope this helps.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 20406106
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...
}
0
 
LVL 2

Author Comment

by:bcmeyer1983
ID: 20406119
help me out here:

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

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

?>
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Author Comment

by:bcmeyer1983
ID: 20406184
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");
}
?>
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 20406336
Yes, your second one looks ok. I forgot about using isset but I would recommend using the $_REQUEST variable
if you are using PHP5.
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20406345
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");
}
?>
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20406388
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.

0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 20406420
Well, I suppose so, but I tend to test my scripts using get. I never thought about it like that.
0
 
LVL 2

Author Comment

by:bcmeyer1983
ID: 20406435
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.
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20406561
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>
0
 
LVL 2

Author Comment

by:bcmeyer1983
ID: 20406580
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
0
 
LVL 21

Accepted Solution

by:
nizsmo earned 275 total points
ID: 20406608
Sorry apologies! This should 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 if(isset($_POST['agree']) && $_POST['agree']=="ON")
{
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>
0
 
LVL 2

Author Comment

by:bcmeyer1983
ID: 20406662
sweet, makes sense now that i see it all. learned a new one. thanks for sharing the expertise!
Kudos
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20407045
no problem glad to help :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question