title: How to use JSON and AJAX with different content of messages in PHPMailer
body: <p>I need help of some advanced users of JSON and AJAX, because I need to use different content of messages in PHPMailer, so this work very well without JSON and AJAX, but I need to display successfull message to user when a form is submitted and can not understand why I get error &quot;Requested JSON parse&quot;?! So, this is part of php code with json.</p>
  <pre><code>    $sql_m = &quot;SELECT owners_email.email_address_id, email_address, owner_name, owner_property, owner_sex, owner_type FROM visitneum.owners_email INNER JOIN visitneum.pages ON (pages.email_address_id = owners_email.email_address_id) WHERE `owner_sex`='M' AND `owner_type`='rooms' AND `pages_id` = ?&quot;;
          
      $dbstmt = $pdo-&gt;prepare($sql_m);
      $dbstmt-&gt;bindParam(1,$pages_id);
      $dbstmt-&gt;execute();
      //get emails from db via pdo
      $emails_room_m = $dbstmt-&gt;fetchAll(PDO::FETCH_ASSOC);
      if(is_array($emails_room_m) &amp;&amp; count($emails_room_m) &gt; 0) {
          foreach ($emails_room_m as $email_room_m){
              $mailOwner-&gt;addAddress($email_room_m['email_address']);
                  $body_room_m = &quot;&lt;p&gt;Poštovani {$email_room_m['owner_name']}, &lt;br&gt;&quot; . &quot;Upravo ste primili poruku sa sajta &lt;a href='https://www.visit-neum.com'&gt;visit-neum.com&lt;/a&gt;&lt;br&gt;Detalji Vaše poruke se nalaze ispod:&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Od: &lt;/strong&gt;&quot; . ucwords($fname) . &quot;&lt;br&gt;&lt;strong&gt;Telefon: &lt;/strong&gt;&quot; . $tel . &quot;&lt;br&gt;&lt;strong&gt;Datum dolaska: &lt;/strong&gt;&quot; . $txtFrom . &quot;&lt;br&gt;&lt;strong&gt;Datum odlaska: &lt;/strong&gt;&quot; . $txtTo . &quot;&lt;br&gt;&lt;strong&gt;E-mail: &lt;/strong&gt;&quot; .strtolower($userMail).&quot;&lt;br&gt;&lt;strong&gt;Poruka: &lt;/strong&gt;&quot; . $userMessage . &quot;&lt;br&gt;&lt;br&gt;&lt;strong&gt;Napomena: &lt;/strong&gt;Molimo Vas da na ovu poruku ne odgovarate. Vaš odgovor pošaljite na: &quot; . strtoupper($userMail) . &quot;&lt;/p&gt;&quot;;
                          $mailOwner-&gt;Body = $body_room_m;
   
      if($mailOwner-&gt;send()){
          $mailOwner = &quot;INSERT INTO visitneum.contact_owner (fname, tel, txtFrom, txtTo, userMail, userMessage, email_address_id) VALUES (:fname, :tel, :txtFrom, :txtTo, :userMail, :userMessage, :email_address_id)&quot;;
          $stmt = $pdo-&gt;prepare($mailOwner);
          $stmt-&gt;execute(['fname' =&gt; $fname, 'tel' =&gt; $tel, 'txtFrom' =&gt; $txtFrom, 'txtTo' =&gt; $txtTo, 'userMail' =&gt; $userMail, 'userMessage' =&gt; $userMessage, 'email_address_id' =&gt; $email_room_m['email_address_id']]);
          if($error==false){
              $data[&quot;response&quot;] = &quot;success&quot;;
              $data[&quot;content&quot;] = &quot;Hvala Vam &quot; . ucwords($fname) . &quot;! Vaša poruka je uspješno poslata vlasniku objekta {$email_room_m['owner_property']}! Odgovor ćete dobiti ubrzo!&quot;;
          }//end if $error==false
      }//end if mail send
  
      }//end foreach for email addresses  (man owners of rooms)
  
      }//end if for array of emails
  
      // SELECT email values from database for woman owners of rooms
      $sql_f=&quot;SELECT owners_email.email_address_id, email_address, owner_name, owner_property, owner_sex, owner_type FROM visitneum.owners_email INNER JOIN visitneum.pages ON (pages.email_address_id = owners_email.email_address_id) WHERE `owner_sex`='F' AND `owner_type`='rooms' AND `pages_id` = ?&quot;;
      
      $dbstmt = $pdo-&gt;prepare($sql_f);
      $dbstmt-&gt;bindParam(1,$pages_id);
      $dbstmt-&gt;execute();
      //var_dump($dbstmt);
      //get emails from db via pdo
      $emails_room_f = $dbstmt-&gt;fetchAll(PDO::FETCH_ASSOC);
      if(is_array($emails_room_f) &amp;&amp; count($emails_room_f) &gt; 0) {
          foreach ($emails_room_f as $email_room_f){
              //var_dump($email_room_f['email_address']);
              $mailOwner-&gt;addAddress($email_room_f['email_address']);
  
                  $body_room_f = &quot;&lt;p&gt;Poštovana {$email_room_f['owner_name']}, &lt;br&gt;&quot; . &quot;Upravo ste primili poruku sa sajta &lt;a href='https://www.visit-neum.com'&gt;visit-neum.com&lt;/a&gt;&lt;br&gt;Detalji Vaše poruke se nalaze ispod:&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Od: &lt;/strong&gt;&quot; . ucwords($fname) . &quot;&lt;br&gt;&lt;strong&gt;Telefon: &lt;/strong&gt;&quot; . $tel . &quot;&lt;br&gt;&lt;strong&gt;Datum dolaska: &lt;/strong&gt;&quot; . $txtFrom . &quot;&lt;br&gt;&lt;strong&gt;Datum odlaska: &lt;/strong&gt;&quot; . $txtTo . &quot;&lt;br&gt;&lt;strong&gt;E-mail: &lt;/strong&gt;&quot; .strtolower($userMail).&quot;&lt;br&gt;&lt;strong&gt;Poruka: &lt;/strong&gt;&quot; . $userMessage . &quot;&lt;br&gt;&lt;br&gt;&lt;strong&gt;Napomena: &lt;/strong&gt;Molimo Vas da na ovu poruku ne odgovarate. Vaš odgovor pošaljite na: &quot; . strtoupper($userMail) . &quot;&lt;/p&gt;&quot;;
                          $mailOwner-&gt;Body = $body_room_f;
   
      if($mailOwner-&gt;send()){
          $mailOwner = &quot;INSERT INTO visitneum.contact_owner (fname, tel, txtFrom, txtTo, userMail, userMessage, email_address_id) VALUES (:fname, :tel, :txtFrom, :txtTo, :userMail, :userMessage, :email_address_id)&quot;;
          $stmt = $pdo-&gt;prepare($mailOwner);
          $stmt-&gt;execute(['fname' =&gt; $fname, 'tel' =&gt; $tel, 'txtFrom' =&gt; $txtFrom, 'txtTo' =&gt; $txtTo, 'userMail' =&gt; $userMail, 'userMessage' =&gt; $userMessage, 'email_address_id' =&gt; $email_room_f['email_address_id']]);
          if($error==false){
              $data[&quot;response&quot;] = &quot;success&quot;;
              $data[&quot;content&quot;] = &quot;Hvala Vam &quot; . ucwords($fname) . &quot;! Vaša poruka je uspješno poslata vlasniku objekta {$email_room_f['owner_property']}! Odgovor ćete dobiti ubrzo!&quot;;
          }//end if $error==false
      }//end if mail send
      }//end foreach for email addresses  (woman owners of rooms)
      }//end if for array of emails
      
      // SELECT email values from database for man owners of others property(restaurants, ships, etc)
      $query_m = &quot;SELECT owners_email.email_address_id, email_address, owner_name, owner_property, owner_sex, owner_type FROM visitneum.owners_email INNER JOIN visitneum.pages ON (pages.email_address_id = owners_email.email_address_id) WHERE `owner_sex`='M' AND `owner_type`='other' AND `pages_id` = ?&quot;;
          
      $dbstmt = $pdo-&gt;prepare($query_m);
      $dbstmt-&gt;bindParam(1,$pages_id);
      $dbstmt-&gt;execute();
      //get emails from db via pdo
      $emails_other = $dbstmt-&gt;fetchAll(PDO::FETCH_ASSOC);
      if(is_array($emails_other) &amp;&amp; count($emails_other) &gt; 0) {
          foreach ($emails_other as $email_other){
              $mailOwner-&gt;addAddress($email_other['email_address']);
                  $body_other = &quot;&lt;p&gt;Poštovani {$email_other['owner_name']}, &lt;br&gt;&quot; . &quot;Upravo ste primili poruku sa sajta &lt;a href='https://www.visit-neum.com'&gt;visit-neum.com&lt;/a&gt;&lt;br&gt;Detalji Vaše poruke se nalaze ispod:&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Od: &lt;/strong&gt;&quot; . ucwords($fname) . &quot;&lt;br&gt;&lt;strong&gt;Telefon: &lt;/strong&gt;&quot; . $tel . &quot;&lt;br&gt;&lt;strong&gt;E-mail: &lt;/strong&gt;&quot; .strtolower($userMail).&quot;&lt;br&gt;&lt;strong&gt;Poruka: &lt;/strong&gt;&quot; . $userMessage . &quot;&lt;br&gt;&lt;br&gt;&lt;strong&gt;Napomena: &lt;/strong&gt;Molimo Vas da na ovu poruku ne odgovarate. Vaš odgovor pošaljite na: &quot; . strtoupper($userMail) . &quot;&lt;/p&gt;&quot;;
                  $mailOwner-&gt;Body = $body_other;
   
      if($mailOwner-&gt;send()){
          $mailOwner = &quot;INSERT INTO visitneum.contact_owner (fname, tel, userMail, userMessage, email_address_id) VALUES (:fname, :tel, :userMail, :userMessage, :email_address_id)&quot;;
          $stmt = $pdo-&gt;prepare($mailOwner);
          $stmt-&gt;execute(['fname' =&gt; $fname, 'tel' =&gt; $tel, 'userMail' =&gt; $userMail, 'userMessage' =&gt; $userMessage, 'email_address_id' =&gt; $email_other['email_address_id']]);
          if($error==false){
              $data[&quot;response&quot;] = &quot;success&quot;;
              $data[&quot;content&quot;] = &quot;Hvala Vam &quot; . ucwords($fname) . &quot;! Vaša poruka je uspješno poslata vlasniku objekta {$email_other['owner_property']}! Odgovor ćete dobiti ubrzo!&quot;;
              }//end if $error==false
      }//end if mail send
      }//end foreach for email addresses  (man owners of other properties(restaurants, ships etc.))
  
  }//end if for array of emails
  else {
      $data['response'] = &quot;error&quot;;
      $data['content'] = &quot;Došlo je do greške! Pokušajte ponovo...&quot; . $mailOwner-&gt;ErrorInfo;
      }
      echo json_encode($data);
  </code></pre>
  <p>And this is jQuery code with AJAX.</p>
  <pre class="lang-js prettyprint-override"><code>submitHandler: function(form) {
    //Your code for AJAX starts    
    var formData = jQuery(&quot;#contactOwner&quot;).serialize();
    console.log(formData);
    /*var data;
    if (typeof(data) == 'undefined') {
        JSON.parse(data);
      }*/
    jQuery.ajax({
      url: &quot;./inc/mail_process.php&quot;,
      type: &quot;post&quot;,
      dataType: &quot;.json&quot;,
      contentType: &quot;application/json&quot;,
      data: formData,
      cache: true,
      success: function(data) {
        //alert(&quot;success&quot;);
        jQuery(&quot;#responseOwner&quot;).text(data.content);
        //console.log(JSON.stringify(data));
        console.dir(&quot;Response: &quot; + data.content);
      },
      /*error: function(data){
          //alert(&quot;error&quot;);
          jQuery(&quot;#responseOwner&quot;).text(&quot;An error occurred&quot;);
           console.dir(&quot;Response: &quot;+data.content);
          //console.log(JSON.stringify(data));
          //console.log(xhr);
      }*/
      error: function(jqXHR, exception) {
        if (jqXHR.status === 0) {
          console.log('Not connect.\n Verify Network.');
        } else if (jqXHR.status == 404) {
          console.log('Requested page not found. [404]');
        } else if (jqXHR.status == 500) {
          console.log('Internal Server Error [500].');
        } else if (exception === 'parsererror') {
          console.log('Requested JSON parse failed.');
        } else if (exception === 'timeout') {
          console.log('Time out error.');
        } else if (exception === 'abort') {
          console.log('Ajax request aborted.');
        } else {
          console.log('Uncaught Error.\n' + jqXHR.responseText);
        }
      }
  
  
    }); //Code for AJAX Ends
    // Clear all data after submit
    var form = document.getElementById('contactOwner').reset();
    return false;
  } //submitHandler
  </code></pre>