I don’t know why, this error suddenly popped-up today and I can’t get my head around it.
Basically, I have dynamically generated input elements in PHP and I like to use javascript to create a timepicker for them, since they are generated using a loop I also need to generate the script using a loop.
In this case I must use an inline JS echo’ed via PHP to avoid using AJAX calls.
This is the javascript code that’s giving me trouble, it works perfect if I hand-code numbers instead of PHP variables… needless to say it worked perfect until some point yesterday, i’m not sure what changed.
Your code appears to be correct. Therefore, I suspect it is one of your variables that is entering garbage into your Javascript routine.
The simplest way to debug this would be to go to that page, RIGHT-CLICK on it and select VIEW-SOURCE.
This will show you the HTML/JS page as it is AFTER the PHP has been processed.
Look at the listing and view the area where this JQuery routine is located. You should see where it breaks and work backwards from there to fix it. If that routine is not working enough to be displayed, you can replace your “ECHO” lines with a “DIE()” routine to kill the page at that point and display the script on the page. Either way, you would be able to view the OUTPUT of the code before it is executed see where it is breaking down.
Yeah, I did all that debugging, the weird part is that even if I reset the values of all the variables to 0 I still get that error!
This is the fifth time i’m sitting down to solve this problem and after I read your reply I was trying again with the same errors so I stopped and thought again what the heck could cause this.
The echo’ed line by line and saw no change so there must be something wrong with the syntax. Eventually I discovered it was this silly line:
[php]$(document).ready(function() {[/php]
I’m not an expert of which data comes first, the JS if that function is used or the variables from PHP, in any case, that’s what caused this silly damn error.
So even if your reply did not contain the answer, it did lead me to think outside of the box for a change and I thank you for that.
Good day
EDIT: Is it good practice to use json_encode for those variables? i’m pretty sure I shouldn’t but whilst I was trying to solve this issue I came across answers about AJAX(about this issue) and some of them said to use json_encode. So, use json_encode only when I actually use JSON?
Glad you figured it out. Sorry I didn’t figure it out myself!
So, Json is actually a way to format the data. Not really a big deal to use it or not.
I usually just use AJAX to pull PHP files and load them dynamically without the JSon.
It is mostly to make it easier to pass data back and forth. Here is a site that has
a nice tutorial on how it should work. Might give you ideas about using it or not.