Ajax events - behaviour on error and lack of notification

After a number of very frustrating hours, I have discovered very painfully that errors that occur in Ajax just stop it from working without reporting an error. Shared so that you might not have the same pain…

A shortened version of what caused this:

Problem:
On onClick event a field {pay} was supposed to update after a calculation, using Ajax and an internal library function. The field would not update. No error was displayed.

Trying to test this:
{pay} = 27;
would update in the Ajax event and show 27
$a = 27;
{pay} = $a;
would just leave the {pay} field blank!

Cause:
There was an error in the function (in this case trying to address a non-existent element in an array).
This still permitted a number of things to work downstream of the error to report on the array or assigned variables e.g.
echo
var_dump()
which was very confusing. It just would not then assign any variables to the fields.

I’m not sure how the best way of troubleshooting this is - mine was painstakingly slow, line by line - but any ideas to be shared about spotting errors in Ajax would be widely appreciated I’m sure.

Hy. When you code in an ajax event the php code is executed asynchronous and in background. If it fails because of a PHP syntax error the calling app has no knowledge of it.
What you can do to debug ajax code is to look into the log of your webserver.
If you use apache see error.log

1 Like

Thanks for that suggestion.

I tried asking my provider for access to the error logs (which unfortunately are held centrally (for all accounts) on cPanel VPS which is what I’m using) so not directly accessible and when I asked tech support they couldn’t find any.

I did suddenly realise that the SC error that seemed to pop-up once only (and didn’t repeat when I saved/recompliled the library multiple times!) might be significant.

Which takes you to this not very helpful message:
A non-numeric value encountered | Script: /home/dutydoct/public_html/scriptcase9/devel/generator/nm_gp_verifica_php.php linha: 132

so I went to look at the specific file - which is not really very helpful either

before realising that it is referring to the line in my internal library code where the error is (and it is calling an undeclared variable). Sorting this out stopped the error and made everything work :slight_smile:

I thought worth sharing this, since I’ve learned so much from other people sharing their challenges and mistakes.