This place has been a real lifesaver at times so thank you all for your efforts in making this place so great. However, I have an issue that I can't find an answer to on here.
I have a website that has a contact form on the home page, and a contact form on some inner pages. I need each one to redirect to a different thank you page (for PPC conversion tracking purposes)
The one on the inner page works fine, submits info and redirects, but the one on the home page only submits info but no redirect. Each form uses a separate php file for the form action and a separate thank you page. Here is the code, can you see a problem? I'm thinking it may be something to do with the headers?
Home Page Code:
<form action="landing-page-form.php" method="post">
<h2>Get A Quote Today!</h2>
<p><label for="name">Name</label><input name="name" id="name"></p>
<p><label for="tel">Tel</label><input name="tel" id="tel"></p>
<p><label for="email">Email</label><input name="email" id="email"></p>
<p><label for="service">Service</label><select name="service" id="service"><option value="Kitchen Design">Kitchen Design</option><option value="Bathroom Design">Bathroom Design</option><option value="Central HEating">Central Heating</option><option value="Appliances">Kitchen Appliances</option><option value="Brochure Request">Free Brochure Request</option><option value="Home Visit Request">Free Home Visit Request</option></select></p>
<p><input type="checkbox" name="chk" id="chk" value="1"> <label class="chk" for="chk">check the checkbox</label></p>
<p><button name="submit" type="submit">Send My Enquiry!</button></p>
</form>
Here is the relating php
<?php if ($_POST) {
header('Content-type: application/json');
$fields = array();
$msg = array();
foreach($_POST as $k=>$v) $_POST[$k] = trim($v);
$pattern = "/^[a-zA-Z0-9][a-zA-Z0-9\.-_]+\@([a-zA-Z0-9_-]+\.)+[a-zA-Z]+$/";
if(preg_match($pattern, $_POST['email'])) $email_ok = true;
else $email_ok = false;
if(!$_POST['email'] || !$email_ok) $fields[] = "email";
if(!$_POST['name']) $fields[] = "name";
if(!$_POST['tel']) $fields[] = "tel";
if(count($fields)) {
$status = "error";
if(!$_POST['name'] || !$_POST['email'] || !$_POST['email']) $msg[] = "Please specify your name, telephone and email address.";
if(!$email_ok) $msg[] = "Provided e-mail address is invalid.";
$message = implode("<br>", $msg);
$response['msgStatus'] = $status;
$response['message'] = $message;
$response['errorFields'] = $fields;
} else {
$mailContent = "Name: {$_POST['name']}
tel: {$_POST['tel']}
E-mail: {$_POST['email']}
Service: {$_POST['service']}
Checkbox: {$_POST['chk']}";
$ok = mail("email@email.com", "Message from Home Page Landing Page Form", $mailContent, "From:<".$_POST['email'].">");
if($ok) {
$response['msgStatus'] = "ok";
header( 'Location: /thanks.php' ) ;
} else {
$response['msgStatus'] = "error";
$response['message'] = "Could not send your request due to an error.";
}
}
echo json_encode($response);
} ?>
Here is the inner page HTML:
<form action="quick-contact-form.php" method="post">
<h6>Quick Contact</h6>
<p><label for="name">Name</label><input name="name" id="name"></p>
<p><label for="tel">Tel</label><input name="tel" id="tel"></p>
<p><label for="email">Email</label><input name="email" id="email"></p>
<p><label for="service">Service</label><select name="service" id="service"><option value="Kitchen Design">Kitchen Design</option><option value="Bathroom Design">Bathroom Design</option><option value="Heating">Central Heating</option><option value="Appliances">Kitchen Appliances</option><option value="Brochure Request">Free Brochure Request</option><option value="Home Visit Request">Free Home Visit Request</option></select></p>
<p><input type="checkbox" name="chk" id="chk" value="1"> <label class="chk" for="chk">check the checkbox</label></p>
<p><button name="submit" type="submit">Send!</button></p>
</form>
And the relating PHP
<?php if ($_POST) {
header('Content-type: application/json');
$fields = array();
$msg = array();
foreach($_POST as $k=>$v) $_POST[$k] = trim($v);
$pattern = "/^[a-zA-Z0-9][a-zA-Z0-9\.-_]+\@([a-zA-Z0-9_-]+\.)+[a-zA-Z]+$/";
if(preg_match($pattern, $_POST['email'])) $email_ok = true;
else $email_ok = false;
if(!$_POST['email'] || !$email_ok) $fields[] = "email";
if(!$_POST['name']) $fields[] = "name";
if(!$_POST['tel']) $fields[] = "tel";
if(count($fields)) {
$status = "error";
if(!$_POST['name'] || !$_POST['email'] || !$_POST['email']) $msg[] = "Please specify your name, telephone and email address.";
if(!$email_ok) $msg[] = "Provided e-mail address is invalid.";
$message = implode("<br>", $msg);
$response['msgStatus'] = $status;
$response['message'] = $message;
$response['errorFields'] = $fields;
} else {
$mailContent = "Name: {$_POST['name']}
tel: {$_POST['tel']}
E-mail: {$_POST['email']}
Service: {$_POST['service']}
Checkbox: {$_POST['chk']}";
$ok = mail("email@email.com", "Message from Website Landing Page Form", $mailContent, "From:<".$_POST['email'].">");
if($ok) {
$response['msgStatus'] = "ok";
header( 'Location: /thankyou.php' ) ;
} else {
$response['msgStatus'] = "error";
$response['message'] = "Could not send your request due to an error.";
}
}
echo json_encode($response);
} ?>
Any and all help, suggestions, feedback appreciated.