OK so I was getting the header already sent error:
Warning: Cannot modify header information - headers already sent by (output started at /home/index.php:22) in /home/public_html/index.php on line 2218
I turned on output_buffering in the php.ini file and now the error does not show, and it redirects to the new page, however the email the script is supposed to send doesn't come through. Any ideas why this would be? The server does support php mail as I have tested this to make sure.
The PHP code is below I am still learning php so please excuse me if there are things that aren't done correctly. I have highlighted line 22 and 2218 if this helps at all...
<?php
$is_questions_filled = true;
$is_details_filled = true;
$does_duplicate_answers_exist = false;
$is_least_most_selected = true;
$error_message= '';
$duplicate_answer_sections = array();
$least_most_not_selected_sections = array();
$all_answers_not_selected_sections = array();
$total_sections = 25;
$total_red = 0;
$total_blue = 0;
$total_yellow = 0;
$total_green = 0;
$dom = new DOMDocument();
?>
<?php
function check_least_most_selection($selection1, $selection2, $selection3, $selection4)
{
$least_selected = false;
$most_selected = false;
if($selection1 == 0)
{
$least_selected = true;
}
elseif($selection1 == 6)
{
$most_selected = true;
}
if($selection2 == 0)
{
$least_selected = true;
}
elseif($selection2 == 6)
{
$most_selected = true;
}
if($selection3 == 0)
{
$least_selected = true;
}
elseif ($selection3 == 6)
{
$most_selected = true;
}
if($selection4 == 0)
{
$least_selected = true;
}
elseif ($selection4 == 6)
{
$most_selected = true;
}
if($most_selected == true && $least_selected == true)
{
return true;
}
else
{
return false;
}
}
?>
<?php
if(isset($_POST["Submit"]))
{
if($_POST["name"] == '' || $_POST["email"] == '')
{
$is_details_filled = false;
}
for ($x = 1; $x <= $total_sections; $x++)
{
$values = array($_POST["red_question_section".$x], $_POST["green_question_section".$x], $_POST["blue_question_section".$x], $_POST["yellow_question_section".$x]);
if (count($values) !== count(array_unique($values))) {
if(isset($_POST["red_question_section".$x]) && isset($_POST["green_question_section".$x])
&& isset($_POST["blue_question_section".$x]) && isset($_POST["yellow_question_section".$x]))
{
$does_duplicate_answers_exist = true;
$duplicate_answer_sections[$x] = true;
?>
<script>
var count = "<?php Print($x); ?>";
var id = "duplicate_answer_sections_".concat(count);
console.log(id);
document.getElementById(id).setAttribute("style", "null");
</script>
<?php
}
}
if((check_least_most_selection($_POST["red_question_section".$x], $_POST["green_question_section".$x], $_POST["blue_question_section".$x], $_POST["yellow_question_section".$x])) == false)
{
if(isset($_POST["red_question_section".$x]) && isset($_POST["green_question_section".$x])
&& isset($_POST["blue_question_section".$x]) && isset($_POST["yellow_question_section".$x]))
{
$is_least_most_selected = false;
$least_most_not_selected_sections[$x] = $x;
?>
<script>
var count = "<?php Print($x); ?>";
var id = "least_most_not_selected_sections_".concat(count);
console.log(id);
document.getElementById(id).setAttribute("style", "null");
</script>
<?php
}
}
if(!isset($_POST["red_question_section".$x]))
{
$is_questions_filled = false;
?>
<script>
var count = "<?php Print($x); ?>";
var id = "red_question".concat(count);
document.getElementById(id).setAttribute("bgcolor", "red");
document.getElementById(id).style.color = "#fff";
var id = "all_answers_not_selected_sections_".concat(count);
document.getElementById(id).setAttribute("style", "null");
</script>
<?php
}
if(!isset($_POST["yellow_question_section".$x]))
{
$is_questions_filled = false;
?>
<script>
var count = "<?php Print($x); ?>";
var id = "yellow_question".concat(count);
document.getElementById(id).setAttribute("bgcolor", "red");
document.getElementById(id).style.color = "#fff";
var id = "all_answers_not_selected_sections_".concat(count);
document.getElementById(id).setAttribute("style", "null");
</script>
<?php
}
if(!isset($_POST["blue_question_section".$x]))
{
$is_questions_filled = false;
?>
<script>
var count = "<?php Print($x); ?>";
var id = "blue_question".concat(count);
document.getElementById(id).setAttribute("bgcolor", "red");
document.getElementById(id).style.color = "#fff";
var id = "all_answers_not_selected_sections_".concat(count);
document.getElementById(id).setAttribute("style", "null");
</script>
<?php
}
if(!isset($_POST["green_question_section".$x]))
{
$is_questions_filled = false;
?>
<script>
var count = "<?php Print($x); ?>";
var id = "green_question".concat(count);
document.getElementById(id).setAttribute("bgcolor", "red");
document.getElementById(id).style.color = "#fff";
var id = "all_answers_not_selected_sections_".concat(count);
document.getElementById(id).setAttribute("style", "null");
</script>
<?php
}
}
if ($is_details_filled == false)
{
?>
<script>
document.getElementById("error_notice").setAttribute("style", "null");
</script>
<?php
}
if ($is_questions_filled == false)
{
?>
<script>
document.getElementById("answers_not_filled_notice").setAttribute("style", "null");
</script>
<?php
}
if ($does_duplicate_answers_exist == true)
{
?>
<script>
document.getElementById("duplicate_answer_notice").setAttribute("style", "null");
</script>
<?php
}
if ($is_least_most_selected == false)
{
?>
<script>
document.getElementById("least_most_selected_notice").setAttribute("style", "null");
</script>
<?php
}
if($does_duplicate_answers_exist == false && $is_details_filled == true && $is_questions_filled == true && $is_least_most_selected == true)
{
foreach($_POST as $key => $value) {
if (strpos($key, 'red_question_section') === 0) {
$total_red += $value;
}
if (strpos($key, 'blue_question_section') === 0) {
$total_blue += $value;
}
if (strpos($key, 'yellow_question_section') === 0) {
$total_yellow += $value;
}
if (strpos($key, 'green_question_section') === 0) {
$total_green += $value;
}
}
$total_red = round((($total_red/150)*100), 2);
$total_green = round((($total_green/150)*100), 2);
$total_yellow = round((($total_yellow/150)*100), 2);
$total_blue = round((($total_blue/150)*100), 2);
$to_email_address = "help@domain.co.uk";
$email_message = "name:".$_POST['name']."\n"
."email:".$_POST['email']."\n"
."Red = ".$total_red."%\n"
."Blue = ".$total_blue."%\n"
."Yellow = ".$total_yellow."%\n"
."Green = ".$total_green."%\n"
;
$email_subject = "Preference Evaluator Submission";
$email_headers = 'From: info@domain.co.uk';
mail($to_email_address, $email_subject, $email_message, $email_headers);
header('Location: success.php');
}
}
?>