我完全是初学者。我试图了解如何修复此错误(未捕获的SyntaxError:位于0的JSON中的意外标记< )以及此代码的工作原理? (访问我的网站:rodcurvelo.com,点击联系人填写我的表格。查看检查,你会看到错误)。
这是我的联系表格:
<form method="post" id="contactForm" name="contactForm">
<input type="hidden" name="subject" value="Message Contact Form">
<div class="form-group">
<label>Your Name (*)</label>
<input type="text" class="form-control form-flat" name="fullname" required>
</div>
<div class="form-group">
<label>Email (*)</label>
<input type="email" class="form-control form-flat" name="email" required>
</div>
<div class="form-group">
<label>Your Message (*)</label>
<textarea class="form-control form-flat" name="message" rows="8" required></textarea>
</div>
<div class="form-group">
<div id="captcha1"></div>
</div>
<div class="form-group ">
<button type="submit" class="btn btn-flat-solid primary-btn" >Send Message</button>
</div>
<div class="form-group">
<div class="preload-submit hidden"><hr/> <i class="fa fa-spinner fa-spin"></i> Please Wait ...</div>
<div class="message-submit error hidden"></div>
</div>
</form>
这是我的PHP发送电子邮件文件:
<?php
session_start();
//configuration
$path = 'uploads';
$filename = (isset($_POST['file']) && $_POST['file'] != '') ?
$_POST['file'] : NULL;
$message = $_POST['message'];
$from = $_POST['email'];
$subject = $_POST['subject'];
$mailto = 'rod@gmail.com';
// check if any attachment
if ($filename) {
$file = $path . "/" . $filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
}
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// main header (multipart mandatory)
$headers = "From: ". $_POST['fullname']." <" . $from . ">" . $eol;
$headers .= 'Reply-To: <'.$from.'>' . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator .
"\"" . $eol . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol . $eol;
// message
$headers .= "--" . $separator . $eol;
$headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$headers .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
$headers .= $message . $eol . $eol;
// attachment
if ($filename) {
$headers .= "--" . $separator . $eol;
$headers .= "Content-Type: application/octet-stream; name=\"" . $filename
. "\"" . $eol;
$headers .= "Content-Transfer-Encoding: base64" . $eol;
$headers .= "Content-Disposition: attachment" . $eol . $eol;
$headers .= $content . $eol . $eol;
$headers .= "--" . $separator . "--";
}
$ress = array('error' => NULL, 'msg' => NULL);
// check captcha first;
if (isset($_SESSION['simpleCaptchaAnswer']) && $_POST['captchaSelection']
== $_SESSION['simpleCaptchaAnswer']) {
//SEND Mail
if (mail($mailto, $subject, "", $headers)) {
$ress['msg'] = "Thanks, I will get back to you ASAP";
} else {
$ress['error'] = "error : email not sent";
}
} else {
$ress['error'] = "Please check your answer!";
}
//respond to json
echo json_encode($ress);