11.07.2015 Views

AJAX and PHP

AJAX and PHP

AJAX and PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

TTClient-Side Techniques with Smarter JavaScriptThe third parameter of open, called async, specifies whether the request should be h<strong>and</strong>ledasynchronously; true means that script processing carries on after the send() method returnswithout waiting for a response; false means that the script waits for a response beforecontinuing processing, freezing the web page functionality. To enable asynchronous processing,you will seed to set async to true, <strong>and</strong> h<strong>and</strong>le the onreadystatechange event to process theresponse from the server.When using GET to pass parameters, you send the parameters using the URL's query string, asin http://localhost/ajax/test.php?param1=x&param2=y. This server request passes twoparameters—a parameter called param1 with the value x, <strong>and</strong> a parameter called param2 with thevalue y.// call the server page to execute the server side operationxmlHttp.open("GET", "http://localhost/ajax/test.php?param1=x&param2=y", true);xmlHttp.onreadystatechange = h<strong>and</strong>leRequestStateChange;xmlHttp.send(null);When using POST, you send the query string as a parameter of the send method, instead of joiningit on to the base URL, like this:// call the server page to execute the server side operationxmlHttp.open("POST", "http://localhost/ajax/test.php", true);xmlHttp.onreadystatechange = h<strong>and</strong>leRequestStateChange;xmlHttp.send("param1=x&param2=y");The two code samples should have the same effects. In practice, using GET can help withdebugging because you can simulate GET requests with a web browser, so you can easily see withyour own eyes what your server script generates. The POST method is required when sending datalarger than 512 bytes, which cannot be h<strong>and</strong>led by GET.In our examples, we will place the code that makes the HTTP request inside a function calledprocess() in the JavaScript file. The minimal implementation, which is quite fragile <strong>and</strong> doesn'timplement any error-h<strong>and</strong>ling techniques, looks like this:function process(){// call the server page to execute the server side operationxmlHttp.open("GET", "server_script.php", true);xmlHttp.onreadystatechange = h<strong>and</strong>leRequestStateChange;xmlHttp.send(null);}This method has the following potential problems:• process() may be executed even if xmlHttp doesn't contain a validXMLHttpRequest instance. This may happen if, for example, the user's browserdoesn't support XMLHttpRequest. This would cause an unh<strong>and</strong>led exception tohappen, so our other efforts to h<strong>and</strong>le errors don't help very much if we aren'tconsistent <strong>and</strong> do something about the process function as well.• process() isn't protected against other kinds of errors that could happen. Forexample, as you will see later in this chapter, some browsers will generate a securityexception if they don't like the server you want to access with the XMLHttpRequestobject (more on security in Chapter 3).48

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!