Re: Ajax query

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]


You do not need to "load" ajax. The object is built into Javascript. But there a newer versions
that the xmlHttpRequestObject.

In general, your ajax request must be initiated by a user event such as mouse click or a keydown
event. So that has to be written in. Also there is a sequence for intiating the ajax request that must
be adhered to. THEN, on the server side there has to be PHP or other serverside code to recognize
and handle the request and return a response if there should be one. The last phase if there is a 
response expected is for Javascript to handle the response.

Mostly, the ajax request is done ASYNCRONISTICALLY. That just means that a response will NOT
cause the browser window to reload. So state can be maintained. In technical terms it means that
the request is done outside of the browsers main thread. 

A page initiating the request can process its response. But that is tricky to program.
It essentially means that the PHP that process the request must exist above the
DTD declaration and 'exit' as soon as it completes the request. It must also call
header() and issue a header indicating the type of response: text, json, whatever.
Then it uses print or echo to sent the response body. Then exit must called to prevent
any html from being generated.

1 header()
2 print (or echo) response body
3 exit

The newer version of asyncronous requests is more difficult and less intuitive.

Here is the result of a Google search for Javascript training.

Here is another for mozilla javascript:

javascript ajax:

> On Sep 8, 2023, at 4:27 PM, gordonisnz@xxxxxxxxx wrote:
> Hello. I'm trying to get a "simple" ajax going, but the half-dozen examples I've tried do not work.
> It Should simply run/activate a PHP script every 5 or 10 seconds, then display the output in the specified DIV. 
> however, when I go to the developer tools, it says there is something wrong with the ajax line 
> (and nothing changes on my screen) 
> Stupid query 1:
>   <link rel="stylesheet" href="/css/font-awesome.min.css" />
> Do I need to 'load' Ajax somehow - like Javascript ? I do not see this in ANY examples/tutorials I see.
> Here are 2 examples I've tried (apparently these are accepted answers to other peoples queries)
> I did the full  and /rotatestats.php  - still same problem.
> ONE:
> <script type="text/javascript">
> setInterval(function()  
> {
>         $.ajax( "/rotatestats.php" ).done(function(res) { <<< PROBLEM LINE
>             document.getElementById("rot").innerHTML = res;
>     })
> }, 
>  3000);
> </script>
> =================
> TWO :-
> <script type="text/javascript">
> function updateShouts(){
>     // Assuming we have #shoutbox
>     $('#rot').load('rotatestats.php');  << PROBLEM LINE
> }
> setInterval( "updateShouts()", 10000 );
> </script>
> {/literal}
>   <div id="rot">
>   NEW:
>   </div>
> Uncaught ReferenceError: $ is not defined
>     at updateShouts (*WORD*:358:5)
>     at <anonymous>:1:1
> The WORD is some HTML output I do previous to the script. its NOT part of the script / div.. 
> apparently it IS refreshing - which is good, but the output isn't appearing
> Are these two nearly there? I'm not sure what the problem is with the PHP script. if I go to the script directly, it displays a random number..  (Once I get it going, I'll fix up the script to do what I want.
> Gordon.

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux