Solved

PHP verify checkbox is checked

Posted on 2007-12-04
14
363 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now