- Impact
- 17
Hi there,
I'm trying to use a php script to send a form through an html page.
I've followed this tutorial: [ame="http://www.youtube.com/watch?v=o4acVCCbJcI"]AJAX Form Submission To Email - YouTube[/ame] and used the author files (php code below).
I've managed to configured the html properly but get always this error:
There was an Invalid JSON:
Warning: Cannot modify header information - headers already sent by (output started at /home/user/public_html/mysite.com/feedback.php:1) in /home/user/public_html/mysite.com/feedback.php on line 43
error due to a parsererror condition.
Is there anything wrong with the code or is something in the hosting configurations?
Beside this example... could you please point me a tutorial with simple jquery/ajax/php script to send a form.
Thanks in advance,
Ed
I'm trying to use a php script to send a form through an html page.
I've followed this tutorial: [ame="http://www.youtube.com/watch?v=o4acVCCbJcI"]AJAX Form Submission To Email - YouTube[/ame] and used the author files (php code below).
I've managed to configured the html properly but get always this error:
There was an Invalid JSON:
Warning: Cannot modify header information - headers already sent by (output started at /home/user/public_html/mysite.com/feedback.php:1) in /home/user/public_html/mysite.com/feedback.php on line 43
error due to a parsererror condition.
Is there anything wrong with the code or is something in the hosting configurations?
Beside this example... could you please point me a tutorial with simple jquery/ajax/php script to send a form.
Thanks in advance,
Ed
PHP:
<?php
sleep(2);
//Sanitize incoming data and store in variable
$name = trim(stripslashes(htmlspecialchars($_POST['name'])));
$email = trim(stripslashes(htmlspecialchars($_POST['email'])));
$message = trim(stripslashes(htmlspecialchars($_POST['message'])));
$humancheck = $_POST['humancheck'];
$honeypot = $_POST['honeypot'];
if ($honeypot == 'http://' && empty($humancheck)) {
//Validate data and return success or error message
$error_message = '';
$reg_exp = "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,4}$/";
if (!preg_match($reg_exp, $email)) {
$error_message .= "<p>A valid email address is required.</p>";
}
if (empty($name)) {
$error_message .= "<p>Please provide your name.</p>";
}
if (empty($message)) {
$error_message .= "<p>A message is required.</p>";
}
if (!empty($error_message)) {
$return['error'] = true;
$return['msg'] = "<h3>Oops! The request was successful but your form is not filled out correctly.</h3>".$error_message;
echo json_encode($return);
exit();
} else {
//send to an email
$to = 'myemailaddress.com';
$from = 'validemailinthehost.com';
$headers = 'MIME-Version: 1.0' . '\n';
$headers .= 'From: $from' . '\n';
$subject = 'Contact Form Submission\n';
$body = 'Name: ' .$name . '\n';
$body .= 'Email: ' .$email . '\n';
$body .= 'Message: ' .$message . '\n';
//send email and return a message to the user
if(mail($to, $subject, $body, $headers)) {
$return['error'] = false;
$return['msg'] = "<p>Thanks for your feedback " .$name .".</p>";
echo json_encode($return);
} else {
$return['error'] = true;
$return['msg'] = "<h3>Oops! There was a problem sending your email. Please try again.</h3>";
echo json_encode($return);
}
}
} else {
$return['error'] = true;
$return['msg'] = "<h3>Oops! There was a problem with your submission. Please try again.</h3>";
echo json_encode($return);
}
?>