I write simple ajax script but it does not work when i type in box page hang-up and reload everything gone to hide
Here is snapshot of mu ajax code
And i want search value i write search script it return all result
Here is snap
Help me please
Got it?
Okay, so HOW-TO debug your Javascript, if you cannot use the alert box?
The answer is: use console.log()
instead!
See my changes at https://github.com/githubertus/Malik_Helpdisk/commit/e97a9ec3bab96d5e8f174d3505c02144ef6451af
I have added a console log to display your search variable, and disabled the alert box to display the data.
Please add these changes to your javascript, too. Then open the Chrome developer console at the bottom with the Network tab enabled and navigate to your Help page. You will get a screen like this:
Have a look at the blue line now: It's important to note that calling your Help page this way gives a page of type 'document' - which is a normal HTML response.
Next, clean your console by clicking the cleaning icon and enter "how to" in your search input field. Since you entered 6 characters, you are getting 6 responses - this time of type "xhr" which means: it is an Ajax response.
Okay, so your Ajax is basically working correctly. It's time to analyse the response now. But how can we - with the alert box disabled? Time to have a closer look into the developer console again. Simply click the last Help on the left side and the Response tab on the right:
And yes: We are getting the same response here as we had in your alert window before!
Okay, as a last step for today, let's switch to the "console" tab now:
That's great - isn't it? We did not interrupt the Ajax communication flow, but we got everything we wanted to know this way.
So, what's next? As we saw, calling the Help page again from Ajax is answered by a complete new Help page. And that is not what you wanted. So what's your idea to proceed?
Very good, Malik!
Almost perfect. You can even more simplify that to
if ($questions) {
......
So, if there are questions available, then do the foreach - otherwise skip the loop.
Ok, now that we have made the first part of your code running error free, let's have a look into the ajax stuff. :)
But before going deeper into it, let's talk about Javascript debugging in general first. You are often using alert()
for that, and that's not a good idea. Let me try to explain why:
Let's assume, you are a famous and really good ping-pong player, winning every match. Now, right in the middle of a match Zetman arrives asking you about your Helpdisk stuff. On turning to Zetman you cannot concentrate on the ping-pong balls of your partner any more. And it's likely you will lose the game. Without that interruption you would have won it.
Thus, just because of an interruption the expected result (Malik will be the winner) has changed to a different result (Malik will be the loser).
The same unexpected results may appear, if you interrupt an Ajax call with an alert box which is waiting until you press "OK". You can not rely that the result will be the same without that interrupt. And so the next thing to learn is:
> Never ever use the alert box to debug Ajax!
Ok know i use following logic
if($questions !== false){
but this also show same alert as before but no error in log file
Hey Malik,
I see you're checking $records
first now, before running into the foreach loop:
That's the right decision! :)
But in order to make your code as fast as possible, it's worth to have a closer look at your constrcut and ask: Do we really need TWO functions here? You are using the if()
function AND the empty()
function. Why?
Your getQuestion()
function finally is making use of searchObject()
. So let's open classes/OssnObject.php and see what will be returned in the end: You'll either get an array of objects on line 481, or FALSE a line below if there are no records found.
In other words: searchObject is already validating whether there is something to return or not. So (back in page.php) that means: If you ARE getting back something here, you can be sure it is NOT empty.
So, you don't need to check emptiness here again!
Instead you can simply reverse your logic like if there are records, then run into the foreach loop
.
Besides that: Always have a look at the manual page before using a function. (see http://php.net/manual/en/function.empty.php)
Especially empty() can be dangerous because it may return unexpected results in some cases.
Example:
$malik = 0;
if (!empty($malik)) {
echo 'malik is not empty';
}
Most likely you would expect getting 'malik is not empty' here, because you actually set the var to something. But '0' will be evaluated to FALSE - so you won't see that message. Surprising, right? :)
But right this is explained in the manual:
Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.
if i record from db its show such error so i fix that but same alert as before
https://github.com/Lablnet/HelpDisk
Malik,
your component should run error-free under ALL conditions.
It can't be a solution to add a record to the db to make the error disappear.
I think it's time to learn how to make use of PHP error reporting, instead :)
Your next exercise:
1. Remove those records from db to make that error appear again.
2. Then add some code to examine the value of the first argument of your foreach loop.
And please update your Github so we are able to follow what you are doing.
> Do you get the same error on your localhost?
I am not getting any error in log file and error reporting on
i think you get this error as you add no question in db try to add some questions
> Done but show alert same as before in my localhost and your newblue
Of course it does - as you had programmed it that way. :)
But let's forget about that alert for now - because it only pops up when you enter something in your search box.
But before you can enter anything into that search box, the Help page must have loaded. So the next question is:
Does the help page load without errors?
And no, it does not. At least on Newblue I get an error in m error_log file like
[05-Dec-2017 09:27:06 Europe/London] PHP WARNING: 2017-12-05 09:27:06 (GMT): "Invalid argument supplied for foreach()" in file /home/ongolito/newblue.ongolito.net/components/Helpdisk/plugins/default/pages/page.php (line 30)
Do you get the same error on your localhost?
> And I very much recommend to revert all your useless changes you made last night and run the same code as you had yesterday and I have on newblue.
Done but show alert same as before in my localhost and your newblue
Really sorry for late reply
okay,
in order to stop confusion and get back in sync
I installed your code from yesterday onto https://newblue.ongolito.net
Additionally I created a respository at https://github.com/githubertus/Malik_Helpdisk
And I very much recommend to revert all your useless changes you made last night and run the same code as you had yesterday and I have on newblue.
Please give a short feedback when you are ready to go on.
Due to the many requests in the past for additonal features and components we have decided to develope a premium version. Features like Hashtags, Videos, Polls, Events, Stories, Link Preview, etc included in it.
$199 (Life Time)