How to fix contact form errors on submit in AMP?

635 Views Asked by At

I tried to submit the form from www.lightmorelight.com but I've got errors in search console: Response must contain the AMP-Access-Control-Allow-Source-Origin header. I can't figure it out

Here is my code:

<?php 
if(isset($_POST['submit'])){

header("HTTP/2 200");
header("access-control-allow-headers: Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token");
header("access-control-allow-credentials: true");
header("access-control-allow-origin: https://lightmorelight.com");
header("amp-access-control-allow-source-origin: https://lightmorelight.com");
header("access-control-allow-methods: POST, GET, OPTIONS");
header("access-control-expose-headers: AMP-Access-Control-Allow-Source-Origin");
$name = $_POST['nume'];
$subject = $_POST['subiect'];
$message = $_POST['mesaj'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$from = "From:". $name ."<". $email .">";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers = "From: ".$name." <".$email.">\r\n";
$headers .= "Reply-To:".$name." <".$email.">\r\n";
$headers .= "Return-Path: lightmorelight.com<[email protected]>\r\n";
     $body = "$message\n\n
$name 
Tel: $tel
Email:$email_field";
mail('[email protected]','www.lightmorelight.com - '.$subject,$body,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
$string = "Thank you ".$name." for getting in touch! \\n\\nI appreciate you contacting me about ".$subject.". I will contact you shortly. \\n\\nHave a great day ahead!";
die("<script>alert(\"$string\");</script><script type=\"text/javascript\">window.location = \"https://www.lightmorelight.com/contact/\";</script>");
}
?>

<form action-xhr="#" method="POST" target="_top">
<input type="text" name="nume" placeholder="Nume" required><br/>
<input type="text" name="tel" placeholder="Tel" required><br/>
<input type="text" name="email" placeholder="Email" required><br/>
<input type="text" name="subiect" placeholder="Subiect" required><br/>
<textarea name="mesaj" placeholder="Mesaj..." rows="6" cols="30"></textarea><br/>
<input type="submit" name="submit" value="Trimite mesaj" class="button">
</form>

Any help would be appreciated!

1

There are 1 best solutions below

0
On

If you're using action-xhr with amp-form, the response returned from your server must be JSON.

Because your response is HTML, the error you're getting is coming from the AMP runtime trying to parse this HTML as JSON.