I am pretty new to this and 'enjoying' some of the frustrations and long hours associated with learning something new. Anyway! I want to get my php script to do the following:
A) See if the form at the bottom of the page (in the footer of each of the pages of the website) has been triggered using a hidden input. B) Open the table in the database and do some stuff to it. C) Depending on the outcome of this, stop processing, forget everything and go to one of two new php pages; either 'thanks for joining' or 'already a member'.
A and B are working splendidly and C is refusing to work no matter what i do. I have tried using header("Location: http://www.xxx.co.uk/xxx.php"); in just about every part of the page and with all the various subtle changes - single quote, double quote, space after Location, etc. that i have seen used on other answers.
If i issue an exit after the header() command then it does this and i just get a totally white page and nothing, if i don't exit then the page that i was just on comes back unchanged.
I have read the other posts on this subject and see that there is a complication with header to do with an error about header already having been called, but i don't understand the answer and I am not seeing an error, it just will not redirect.
I have tried moving all the php to the top of the page before even the opening doctype declaration and still no dice.
Could it be something to do with the structure of my pages which is as follows:
Each php starts with an include_once for a header.php and ends with an include_once for a footer.php. Inside the header I start with an include_once for a php fuctions file and then some html and then check to see if my footer form was used.
Can somebody explain the limitation with the header() command in simple terms so that i can see whether i am going to be able to use it or whether i will need to redesign my whole strategy.
Here is some code:
One of the pages:
<?
include_once ("includes/header.php");
?>
HTML distinct to page
<?
include_once ("includes/footer.php");
?>
The Header file:
<? include_once("includes/functions.php"); ?>
In here is just the logon script for the database and a global variable for the logon...
<?
if (isset($_POST['submitfoot'])) {
Code to send me an email to tell me what is happening...
/* log on to database */
openDB();
/*tidy fields and check for existing record */
$tidyemail = trim($_POST['footemail']);
$tidyname = trim($_POST['footname']);
$sql_email = "SELECT id FROM subscribers WHERE email = '".$tidyemail."'";
$check_res = mysqli_query($mysqli, $sql_email) or die(mysqli_error($mysqli));
if (mysqli_num_rows($check_res) < 1) {
/* free result add the new record */
mysqli_free_result($check_res);
$add_sql = "INSERT INTO subscribers (name,email,date,sub) VALUES('".$tidyname."','".$tidyemail."',CURDATE(),1)";
$add_res = mysqli_query($mysqli, $add_sql) or die(mysqli_error($mysqli));
/* redirect to thanks page */
header("Location: http://www.xxx.co.uk/thanks.php");
} else {
/* redirect to already subscribed page */
header("Location: http://www.xxx.co.uk/subalready.php");
}
}
?>
Then there is the rest of the HTML including js script headers, page header, page body with common menu system for all pages etc.
I realise that the code here on the forum is a mess, but it is much neater in the actual files, so apologies for any difficulty.
Thank you in advance for any guidance that will stop me from going insane with this problem.
EDIT
Thank you showdev - i now have enabled errors as you recommended: I get this:
Warning: Cannot modify header information - headers already sent by (output started at /homepages/9/d302209163/htdocs/includes/header.php:9) in /homepages/9/d302209163/htdocs/includes/header.php on line 59
So this is the problem that previous questions have faced i beleive. Can anybody, gently, tell me how to sort this out please.
Thanks...