0

I am totally beginner. I am trying to understand how can I fix this error (Uncaught SyntaxError: Unexpected token < in JSON at position 0) and how this code works? (Access my website: rodcurvelo.com, click contact e fill out my form. See the inspection and you will see the error).

That's my contact form:

<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>

That's my PHP sending email file:

<?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);
RiggsFolly
  • 83,545
  • 20
  • 96
  • 136
  • Turn on full php error reporting and notices. https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – IncredibleHat Mar 19 '18 at 15:14
  • Every time I've seem this error message it has always been JS trying to parse HTML as JSON. If you look at the response from your contact form its returning: """
    Warning: mail(): Multiple or malformed newlines found in additional_header in /home/rodcurvelo/public_html/php/sending_mail.php on line 43
    {"error":"Please check your answer!","msg":null} """. I've found this potential solution. https://stackoverflow.com/questions/30887610/error-with-php-mail-multiple-or-malformed-newlines-found-in-additional-header. My guess is each header should contain one $eol.
    – Karl Graham Mar 19 '18 at 15:56
  • Thanks Karl Graham, I used $eol once for each row and now it works – Rodricck Curvelo Mar 22 '18 at 20:56

0 Answers0