Jump to content

ajax example...


interpim

Recommended Posts

OK... im having trouble interpreting the tutorials I have read. 

 

Could someone point me to a tutorial, or show me and example of a page with a single image element which that image can be changed based on what is in a folder...

 

for example... i have an image css box.  the image in that box can be changed by either of 2 buttons, one is a forward and the other is a back. 

 

All I want to do is cycle through the images in that directory.

 

Any help is appreciated.  I know PHP, and am weak on Javascript. 

Link to comment
https://forums.phpfreaks.com/topic/130346-ajax-example/
Share on other sites

I've tried the Ajax Tutorials on W3 Schools...

 

the problem i think i am running into is, I don't understand how to pass data back and forth from client to server.

 

I can build a function in php to do what i need.  How do I pass data from the javascript clientside to the php script?  do i need a seperate php file for each function? or can I build several functions in php on 1 file, and call them by name with the javascript?

Link to comment
https://forums.phpfreaks.com/topic/130346-ajax-example/#findComment-676072
Share on other sites

OK, here's your standard HTTP object creation script in JS:

function getHTTPObject() {
    var xmlhttp;
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}
var http = getHTTPObject();

 

Then, here's a function that sends information to a PHP page and processes the results as more JS:

function ajaxFunction(somevar){
http.open('GET', 'ajax.php?somevar='+somevar, true);
http.send(null);
http.onreadystatechange=function(){
	if(http.readyState==4){
		eval(http.responseText); // this will try to execute the returned code from ajax.php as more JavaScript
	}
}
}

Or, you can print out data returned from ajax.php as HTML:

function ajaxFunction(somevar){
http.open('GET', 'ajax.php?somevar='+somevar, true);
http.send(null);
http.onreadystatechange=function(){
	if(http.readyState==4){
		document.getElementById('somediv').innerHTML = http.responseText; // this will put the returned code from ajax.php into a div with id "somediv"
	}
}
}

Link to comment
https://forums.phpfreaks.com/topic/130346-ajax-example/#findComment-676100
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.