Similar questions have been asked, but they don't relate to this scenario as far as I can see. I have a form linked up with a PHP script (below), that sends an e mail and writes the submitted info to Hubspot.
When I submit through the form. it gives me, Cannot modify header information – headers already sent on the line marked in bold toward the bottom (scan for ERROR HAPPENS BELOW). When I remove that header('Location: index.php?vm=Your Message has been submitted. ' . $hubspot_message); line, the error goes away and the form functions correctly without redirecting, which is obviously less than ideal.
Does anyone know what the issue is? I'm so stuck.
<table>
<?php
$email_to = "email@email.com";
function died($error) {
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$first_name = clean_string($_POST['q1']); // required
$last_name = clean_string($_POST['q2']); // required
$email_from = clean_string($_POST['q3']); // required
$telephone = clean_string($_POST['q4']); // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from))
{
$error_message .= 'The Email Address you entered does not appear to be valid.<br>';
}
//}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br>';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br>';
}
$string_exp = "/^[0-9-+()\s]+$/";
if(!preg_match($string_exp,$telephone)){
$error_message .= 'The phone number ' . $telephone . ' you entered does not appear to be valid.<br>';
}
if(strlen($error_message) > 0) {
header('Location: index.php?vm='.$error_message.'Please correct and resubmit' . '&first_name='.$first_name .'&last_name='. $last_name . '&email_from=' . $email_from . '&telephone='. $telephone . '&comments=' . $comments . '&subject=' . $subject);
died($error_message);
}
$hubspot_message = "";
//Translate to Hubspot field names
$arr = array(
'properties' => array(
array(
'property' => 'email',
'value' => $email_from
),
array(
'property' => 'firstname',
'value' => $first_name
),
array(
'property' => 'lastname',
'value' => $last_name
),
array(
'property' => 'phone',
'value' => $telephone
)
)
);
$post_json = json_encode($arr);
//Hubspot API Key
$hapikey = "126wf72-c560-4e11-a2dc-5s88s8wwfd8"; //<-- Your Hubspot API key must be here...
$endpoint = 'https://api.hubapi.com/contacts/v1/contact?hapikey=' . $hapikey;
$ch = @curl_init();
@curl_setopt($ch, CURLOPT_POST, true);
@curl_setopt($ch, CURLOPT_POSTFIELDS, $post_json);
@curl_setopt($ch, CURLOPT_URL, $endpoint);
@curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = @curl_exec($ch);
$status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_errors = curl_error($ch);
@curl_close($ch);
if(strlen($curl_errors)> 0)
{
$hubspot_message .= "</br>curl Errors: " . $curl_errors . "</br>";
}
if($status_code == '409')
{
$hubspot_message .= " Contact with this email address already exists.";
}
if($status_code == '200')//New Contact was accepted by Hubspot
{
$email_message = "Form details below.\n\n";
// email content
$email_message .= "First Name: ".$first_name."\n";
$email_message .= "Last Name: ".$last_name."\n";
$email_message .= "Email: ".$email_from."\n";
$email_message .= "Telephone: ".$telephone."\n";
$email_message .= "Subject: ". "Contact Request Form Submission" ."\n";//<--- Change email subject here
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
//ERROR HAPPENS BELOW - It's supposed to redirect back to contact page and display status
header('Location: index.php?vm=Your Message has been submitted. ' . $hubspot_message);
//echo $hubspot_message . " succeeded";
}
else
{
//echo $hubspot_message . " failed";
if (isset($_SERVER["HTTP_REFERER"])) {
header("Location: index.php?vm=" . $hubspot_message . '&first_name='.$first_name.'&last_name=' .$last_name. '&email_from=' .$email_from. '&telephone='.$telephone);
}
//Redirect back to contact page and display status
//header('Location: example.html');
}
?>
</table>