Ajax and ossn object not work

Malik Umer Farooq Posted in Technical Support 6 years ago

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
enter image description here

And i want search value i write search script it return all result
Here is snap
enter image description here

Help me please

Replies
German Michael Zülsdorff Replied 6 years ago

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:
https://newblue.ongolito.net/demofiles/discussion-2675/Helpdisk-Help.png


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.

https://newblue.ongolito.net/demofiles/discussion-2675/Help-Input.png


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:

https://newblue.ongolito.net/demofiles/discussion-2675/Helpdisk-Response.png


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:

https://newblue.ongolito.net/demofiles/discussion-2675/Helpdisk-ConsoleLog.png


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?

German Michael Zülsdorff Replied 6 years ago

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!

pk Malik Umer Farooq Replied 6 years ago

Ok know i use following logic

if($questions !== false){

but this also show same alert as before but no error in log file

German Michael Zülsdorff Replied 6 years ago

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.

pk Malik Umer Farooq Replied 6 years ago

if i record from db its show such error so i fix that but same alert as before
https://github.com/Lablnet/HelpDisk

German Michael Zülsdorff Replied 6 years ago

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.

pk Malik Umer Farooq Replied 6 years ago

> Do you get the same error on your localhost?

I am not getting any error in log file and error reporting on

enter image description here

i think you get this error as you add no question in db try to add some questions

German Michael Zülsdorff Replied 6 years ago

> 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?

pk Malik Umer Farooq Replied 6 years ago

> 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

German Michael Zülsdorff Replied 6 years ago

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.

Premium Version

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)
Learn More

Other Questions