[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP verify checkbox is checked

Posted on 2007-12-04
14
Medium Priority
?
380 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:Eddie Shipman
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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:Eddie Shipman
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:Eddie Shipman
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month17 days, 18 hours left to enroll

830 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