469,275 Members | 1,818 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

Terms and Conditions Form, 1 Checkbox redirect to another page

Hi I have a basic form, all it has on it is a checkbox with terms and conditions, the user has to check the box to say I agree, the problem I am having is the page is echoing the correct information out but is not redirecting the user if they check the box.

The Php code is in include statement in the form page which is layed out via css etc etc.

The code I have is

Expand|Select|Wrap|Line Numbers
  1. $submit = $_POST['submit'];
  2. $agree = $_POST['agree'];
  3.  
  4. if (isset ($_POST['agree'])) {
  5.  
  6.     echo "<h3>You agree to our terms</h3>";
  7.     header('Refresh: 10; URL=searcher.php');
  8.  
  9. } else echo "<h3>You Disagree, you must agree before using our database</h3>";
  10.  
  11.  
I have also tried

Expand|Select|Wrap|Line Numbers
  1. $submit = $_POST['submit'];
  2. $agree = $_POST['agree'];
  3.  
  4. if (isset ($_POST['agree'])) {
  5.  
  6.     echo "<h3>You agree to our terms</h3>";
  7.     header('Location: searcher.php');
  8.  
  9. } else echo "<h3>You Disagree, you must agree before using our database</h3>";
I have tried using the variables set but nothing works when I do

Can anyone tell me where I am going wrong?

Be grateful for some help as I am getting nowhere fast with this.

Thanks in advance.
Mar 12 '11 #1
16 5320
  1. Make sure your form is set to submit using HTTP POST, not GET. (method="post")
  2. Make sure you checkbox is named "agree" (name="agree") and have some non-empty value (value="AGREED")
  3. In the back-end code check if the value is empty or not.

Expand|Select|Wrap|Line Numbers
  1. if(empty($_POST['agree'])) {
  2.     //Not agreed
  3. }
  4. else {
  5.     //Agreed
  6. }
Mar 13 '11 #2
Thanks for the reply but I am missing something, what you have given me is half working i9n that it redirects to the page required but it also redirects if checkbox not ticked, what have I missed?

Expand|Select|Wrap|Line Numbers
  1. <form id="form1" name="form1" method="post" action="search.php">
  2. <label for="agree">Agree</label>
  3.           <input type="checkbox" name="agree" id="agree" />
  4.  
  5. <input type="submit" name="submit" id="submit" value=" Agree " />
The page with the form is search.php so it is posting back to itself

the php script included in the form is

Expand|Select|Wrap|Line Numbers
  1. <?php include 'php/termscond.php'; ?>
and the script is as follows now

Expand|Select|Wrap|Line Numbers
  1. if(empty($_POST['agree'])) {
  2.  
  3.     echo "<h3>You must agree before accessing our database</h3>";
  4. }
  5. else {
  6.     header('Location: searcher.php');
  7. }
The header is not working although the search.php and searcher.php are at same level in directory in same folder.

Thanks in advance for your help
Mar 13 '11 #3
Code you have posted above works fine. Maybe some other part of the code is interfering with the redirection.

Here's what I tested:
search.php
Expand|Select|Wrap|Line Numbers
  1. <?php include 'termscond.php'; ?>
  2. <form id="form1" name="form1" method="post" action="search.php">
  3. <label for="agree">Agree</label>
  4.           <input type="checkbox" name="agree" id="agree" />
  5.  
  6. <input type="submit" name="submit" id="submit" value=" Agree " />
  7. </form>
termscond.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if(empty($_POST['agree'])) {
  3.  
  4.     echo "<h3>You must agree before accessing our database</h3>";
  5. }
  6. else {
  7.     header('Location: searcher.php');
  8. }
Mar 13 '11 #4
So in your test when you did not check the box you were advised to check or you could not use the search facility but you were kept on the terms page?

When you checked the box you were redirected?
Mar 13 '11 #5
yes, it only goes to the searcher.php if the checkbox is checked.
Mar 13 '11 #6
Would it be possible for you to just have a quick look at the pages concerned, to see if you can spot why it is not working for me then please?
Mar 13 '11 #7
Sure, Please post the complete and latest code you are trying out.
Mar 13 '11 #8
Link to search page

The php code is exactly what you suggested earlier, I just cannot seem to work out why I cannot get it working especially as you have said it worked for you.
Mar 13 '11 #9
Your HTML form seems to be ok. I think the problem might be in http://www.da-ict.co.uk/php/termscond.php
Please post the complete PHP code of that file.
Mar 13 '11 #10
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. if(empty($_POST['agree'])) {
  3.  
  4.     echo "<h3>You must agree before accessing our database</h3>";
  5. }
  6. else {
  7.     header('Location: searcher.php');
  8. }
  9.  
  10.  
  11. ?>
Thanks for this, especially when you said you had it working it threw me even more.
Mar 13 '11 #11
Hmm, that's the same code I tried on my local server and it worked. But I clearly see it's not working on yours.

Can you add error_reporting(E_ALL); on top of all the pages involved in this process and see if you are getting any errors?
And, instead of the redirect echo some text and see of the program flow reaches that section.

Oh, and add a value to your check box.
Expand|Select|Wrap|Line Numbers
  1. <input type="checkbox" id="agree" name="agree" value="yes">
Mar 14 '11 #12
As the rest of the site is being tested I have re-saved the terms and conditions page under http://www.da-ict.co.uk/searchtcs.php until the issue is resolved.

I have put the value="yes" in the checkbox.

If I echo text out instead of the redirect the action works, the correct text is echoed out depending on the condition so does that point to the header('Location: searcher.php'); of the php script?

If you look at http://www.da-ict.co.uk/searchtcs.php I have just left echo text statements in place depending on the checkbox condition the correct one is output but when I put the header back in that just doesnt work.
Mar 14 '11 #13
The error log shows the following

Expand|Select|Wrap|Line Numbers
  1. [14-Mar-2011 13:29:03] PHP Warning:  Cannot modify header information - headers already sent by (output started at /home/daictcou/public_html/searchtcs.php:4) in /home/daictcou/public_html/php/termscond.php on line 22
  2. [14-Mar-2011 13:29:08] PHP Warning:  Cannot modify header information - headers already sent by (output started at /home/daictcou/public_html/searchtcs.php:4) in /home/daictcou/public_html/php/termscond.php on line 22
  3. [14-Mar-2011 13:31:00] PHP Warning:  Cannot modify header information - headers already sent by (output started at /home/daictcou/public_html/searchtcs.php:4) in /home/daictcou/public_html/php/termscond.php on line 22
  4. [14-Mar-2011 13:31:31] PHP Warning:  Cannot modify header information - headers already sent by (output started at /home/daictcou/public_html/searchtcs.php:4) in /home/daictcou/public_html/php/termscond.php on line 22
  5. [14-Mar-2011 13:31:39] PHP Warning:  Cannot modify header information - headers already sent by (output started at /home/daictcou/public_html/searchtcs.php:4) in /home/daictcou/public_html/php/termscond.php on line 22
  6. [14-Mar-2011 13:36:04] PHP Warning:  Cannot modify header information - headers already sent by (output started at /home/daictcou/public_html/searchtcs.php:4) in /home/daictcou/public_html/php/termscond.php on line 22
  7. [14-Mar-2011 13:36:08] PHP Warning:  Cannot modify header information - headers already sent by (output started at /home/daictcou/public_html/searchtcs.php:4) in /home/daictcou/public_html/php/termscond.php on line 22
This is obviously when I am using the header rather then the echo that is currently on the page.
Mar 14 '11 #14
When you redirect using header() there shouldn't be any output generated before that.
Make sure you don't have any echo or HTML output before the redirect.

You can try buffering the output to avoid this http://php.net/manual/en/function.ob-start.php

also place an exit() right after the header()
Mar 14 '11 #15
Sudaraka, thank you very much for your help with this, it was driving me mad, that seems to have fixed it.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. ob_start();
  3. session_start();
  4. ?>
at the top of the form page

the php looks like

Expand|Select|Wrap|Line Numbers
  1. if(empty($_POST['agree'])) {
  2.  
  3.     echo "<h3>You must agree before accessing our database</h3>";
  4. }
  5. else {
  6.     //echo "<h3>You agree with our terms</h3>";
  7.     header('Location: ../searcher.php');
  8.  
  9. }
and it seems to bge working now.

Once again thanks
Mar 14 '11 #16
Good to hear you got it working, glad to help.
Good luck with rest of the project.
Mar 14 '11 #17

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Nazir | last post: by
4 posts views Thread by Andrea De Santi | last post: by
2 posts views Thread by KFactor | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.