why it’s impossible to get values of json_encode in jQuery AJAX call

  ajax, javascript, jquery, json, php

I use the PHP json_encode function inside a foreach loop between two if statements. However, without the jQuery file, all forms are submitted correctly and all data is inserted into db and in place of jQuery AJAX response I get valid JSON like:

        "6":"Hvala Vam Asdfghjkl! Vasa poruka je uspjesno poslata vlasniku objekta Hotel Mampas! Odgovor cete dobiti ubrzo!"

But if I include jQuery then the code inside the foreach loop isn’t executed when using XDebug with vsCode.

I have already lost 1000 hours debugging this code and can not understand where the problem is.

So, please explain to me if my PHP file is OK and where is the error, because I cannot find on a Google search how to use json_encode to send mail to email addresses stored in the database.

Whatever try I usually get error:

AJAX error: parsererror : SyntaxError: Unexpected end of JSON input

Below you can see part of my PHP and jQuery code which for some reason doesn’t work.


$query = "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` = ?";
    $dbstmt = $pdo->prepare($query);
    //get emails from db via pdo
    $emails_other = $dbstmt->fetchAll(PDO::FETCH_ASSOC);
        if(is_array($emails_other) && count($emails_other)>0){
        foreach($emails_other as $email_other){
            $body_other = "<p>Poštovani {$email_other['owner_name']}, <br>" . "Upravo ste primili poruku sa sajta <a href='https://www.visit-neum.com'>visit-neum.com</a><br>Detalji Vaše poruke se nalaze ispod:</p><p><strong>Od: </strong>" . ucwords($fname) . "<br><strong>Telefon: </strong>" . $tel . "<br><strong>E-mail: </strong>" .strtolower($userMail)."<br><strong>Poruka: </strong>" . $userMessage . "</p>";
            $mailOwner->Body = $body_other;
                $mailOwner = "INSERT INTO visitneum.contact_owner(fname, tel, userMail, userMessage, email_address_id) VALUES(:fname, :tel, :userMail, :userMessage, :email_address_id)";
                $stmt = $pdo->prepare($mailOwner);
                $stmt->execute(['fname' => $fname, 'tel' => $tel, 'userMail' => $userMail, 'userMessage' => $userMessage, 'email_address_id' => $email_other['email_address_id']]);
$data['content'][$email_other['email_address_id']]="Hvala Vam ".ucwords($fname)."! Vasa poruka je uspjesno poslata vlasniku objekta {$email_other['owner_property']}! Odgovor cete dobiti ubrzo!";
$jsonData[] = $data;
        $data['content']="Došlo je do greške! Pokušajte ponovo..." . $mailOwner->ErrorInfo;
    }//end foreach for email addresses
    //header("Content-type: application/json;charset=utf-8");
echo json_encode($jsonData, true);
}//end if for array of emails

and this is part of jQuery file with AJAX call

submitHandler: function(form){
      //Your code for AJAX starts    
      var formData=jQuery("#contactOwner").serialize();
        success:function(jsonData) {
         error:function (jqXHR, textStatus, errorThrown){
          console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
      });//Code for AJAX Ends
// Clear all data after submit
      var resetForm = document.getElementById('contactOwner').reset();
      return false;
    } //submitHandler

Source: Ask PHP