?
Solved

PHP verify checkbox is checked

Posted on 2007-12-04
14
Medium Priority
?
376 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 
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 1100 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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

762 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