I'm using this form in an HTML page and it works on 2 sites. I've copied the code below to create contact.html and contact.php
I've tried for 2 days to get this to work on a new site and keep getting an error message:
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in D:\Hosting\7131065\html\contact.php on line 31
Along with the response from the PHP:Contact form successfully submitted.
Yet the '\' does not throw and error on other sites. Removing '\' results in a different error.
The response should appear (via AJAX) in contact.html but instead the response is displaying page contact.php
HTML form (including links)
CSS
HTML
JS
contact.php
// Set up
$from = 'new-contact@tsagl.org/';
$sendTo = 'contact-manager@tsagl.org';
$subject = 'New message from Conact page';
$fields = array('yourname' => 'Name', 'email' => 'Email', 'hear' => 'How did you hear about us?', 'message' => 'Message');
$okMessage = 'Contact form successfully submitted.';
$errorMessage = 'There was an error while submitting the form. Please try again later';
// Actions
if (empty($_POST['message'])) {
exit;
}
try
{
$emailText = "New message from Contact Page\n___________________________\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
mail($sendTo, $subject, $emailText, "From: " . $from);
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
Any response greatly appreciated!
No comments:
Post a Comment