0

I have this ajax request:

$.ajax({
    type: 'POST',
    url: 'contactUsInsertBoat.php',
    data: {
        name: $insertBoatForm.find( 'input[name=name]' ).val(),
        phone: $insertBoatForm.find( 'input[name=phone]' ).val(),
        email: $insertBoatForm.find( 'input[name=email]' ).val(),
    },
    success:function(data){
        // successful request;
        var json_obj=JSON.parse(data);
        var $insertBoatContent = $( '#insert-boat-content' );
        $insertBoatContent.addClass("center");
        if (json_obj["response"] == true) {
            $insertBoatContent.html( "<br/><h4>Richiesta inviata con successo</h4>" );
        } else {
            $insertBoatContent.html( "<br/><h4>Richiesta non inviata</h4>" );
        }
    },
    error:function(){
        // failed request; give feedback to user
        $('#ajax-panel').html('<p class="error"><strong>Oops!</strong> Try that again in a few moments.</p>');
    }
});

In contactUsInsertBoat.php I do this:

include 'functions.php';
require_once 'BusinessLogic/Manager.php';

use BusinessLogic\Manager;

sec_session_start();

if(isset($_POST['name'])){
    Manager::contactInsertBoats($_POST['name'],$_POST['email'],$_POST['phone']);
    $arrResult = array ('response'=>true);
    echo json_encode($arrResult);
}else{
    $arrResult = array ('response'=>false);
    echo json_encode($arrResult);
}

But after that I do this firebug console writes:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data


var json_obj=JSON.parse(data);

and the content of JSON is:

<br />
<b>Strict Standards</b>:  Non-static method BusinessLogic\Manager::sendMail() should not be called statically in <b>/var/www/public/BusinessLogic/Manager.php</b> on line <b>1957</b><br />
{"response":true}

Why is it adding the strict standards log to JSON object?

kmoser
  • 8,780
  • 3
  • 24
  • 40
Giuseppe87
  • 406
  • 4
  • 14
  • 3
    Its like the error says, you are calling `sendMail()` as static method and it's not. The problem is in your Manager.php file. – TheDrot May 18 '16 at 16:34
  • @TheDrot Maybe I wasn't clear enough: I don't understand why is putting this message in the answer of the JSON obj. However it is not an error: it is just a "Strict Standards" message that does not block the execution – Giuseppe87 May 18 '16 at 16:37
  • 2
    Well then add `ini_set('display_errors', '0');` at the top to hide errors. But I think you should fix these things. – TheDrot May 18 '16 at 16:40
  • @TheDrot thank you very much, it solved the problem :) I would like to know why the log errors go inside the ajax data response, do you know? – Giuseppe87 May 18 '16 at 16:44
  • I have undestand the reason: it takes everything is printed in php file with or without echo – Giuseppe87 May 18 '16 at 16:48
  • 1
    Because the error/notices get echoed as html, which along with your json it passes it back to ajax response. – TheDrot May 18 '16 at 18:30

2 Answers2

2

Because you have error reporting set to E_ALL and display_errors to "On" or 1, which displays all errors including E_STRICT to the output

http://php.net/manual/ro/function.error-reporting.php

Best practice is to log all but disable sending errors to output from php.ini :

display_errors = Off

This is the response given before: Stop printing php error messages to browser

Community
  • 1
  • 1
  • This should be the accepted answer, since this is the general answer to the original question. Of course fixing the real cause of the error is important, but that is not the point here. – Bbak Mar 08 '17 at 19:13
2

The method contactInsertBoats is not a static method in class Manager. So, you need to make an instance of Manager and then, call the method.

Please make a simple change in your PHP code:

include 'functions.php';
require_once 'BusinessLogic/Manager.php';

use BusinessLogic\Manager;

sec_session_start();

if(isset($_POST['name'])){
$manager = new Manager;    
$manager->contactInsertBoats($_POST['name'],$_POST['email'],$_POST['phone']);
    $arrResult = array ('response'=>true);
    echo json_encode($arrResult);
}else{
    $arrResult = array ('response'=>false);
    echo json_encode($arrResult);
}

However, you can set error_reporting(0); at the beginning of your PHP file to prevent showing notices/warnings/errors/...

Mojtaba
  • 4,852
  • 5
  • 21
  • 38