A lot of web services provide you with some useful JSON formatted data in exchange for a few parameters or just some love, and this is a particularly simple and easy method to grab some of that JSON goodness for use in your own PHP page. In order to grab the data, we’ll first create a HTTP stream content where we’ll add the all important X-Request-With = XMLHttpRequest header that is used to identify requests as being AJAX requests. At this point you can add any parameters you wish to hit the web service with by adding them as an associative array and wrapping them in a http_build_query() call for the context content. The next step is to use the classic file_get_contents (this assumes your apache/PHP allows the use of the function to open URLs) to hit the target URL using the newly created context. This should work, resulting in a JSON object which you can use PHP’s json_decode function to manipulate. In action: $opts = array(‘http’ => array( ‘method’ => ‘POST’, ‘header’ => “Content-type: application/x-www-form-urlencoded\r\nX-Requested-With: XMLHttpRequest\r\n”, ‘content’ => http_build_query(array()) ) ); $context = stream_context_create($opts); $result = file_get_contents(“http://webservice.org/route/list/key/mykey/format/json”, false, $context); $jsondata = json_decode($result, true); Nifty.
JSON has become just as big as XML in terms of being the format used to transfer data between AJAX driven web services, meaning that as a PHP developer you will no doubt be doing a lot of dealing with JSON objects. The default way of grabbing JSON data returned by a web service is to call the built in json_decode function which generates what is known as a stdClass. These can be traversed much as you would a normal array using loops: $jsondatastdclass = json_decode($jsoninput); print_r($jsondataarray); foreach ($jsondatastdclass as $object) { foreach ($object as $property=>$value) { echo $property.” has the value “. $value; } } It’s not great, but it does work. However, later version of PHP do allow you to add a second parameter when calling the json_decode function, namely a boolean which then forces the function to return a normal associative array. In practice: $jsondataarray = json_decode($jsoninput, true); print_r($jsondataarray); echo $jsondataarray['data'][0]; And now you know.

With the release of the powerful jQuery Mobile 1.0 framework, it is now becomes possible for pretty much anyone to crank out an uber slick HTML 5 website that looks like a ‘native app’ on just about any mobile device. However, one of the things that does break on implementation is the trusty old form file upload element, the result of the nifty AJAX-driven page load system that jQuery Mobile uses. (AJAX doesn’t allow file transfers as a security feature). Luckily for us though, there is a way to re-enable form file uploads with the only sacrifice being the slick page load, which when you think of it, isn’t such a big sacrifice now that you have the ability to launch files up to your web service once more! In short, what we want to do is instruct the jQuery Mobile framework to not process the form submit event as an AJAX call. To do this, the following “data-ajax” element property needs to be added to your form declaration: <form method=”POST” enctype=”multipart/form-data” action=”process_update.php” data-ajax=”false”> Simple as that.

Today’s quick code tip looks at how one would send all user’s selections made in a multiple select box to a processing script using a jQuery AJAX call.

Because the DataTables was being fed through a server-side script, in other words the actual table data is loaded by the DataTables plugin itself, my jQuery function that was supposed to interact with the clicking of a smiley face couldn’t work because the elements it was meant to interact with simply didn’t exist when the page’s initial DOM tree was being loaded.

In order to execute better security, javascript does not allow for ajax queries to be directed at a different domain from the one on which the ajax call was originally made.

Rather, I want to highlight how one easily posts all of a form’s various values (housed in textboxes, text areas, selects or even radio buttons) to one’s jQuery ajax function call.

The well-established javascript library jQuery comes bundled with a neat little function that handles loading remote content directly into the current page’s DOM model, namely the little Ajax load function.
Craig Lotter is an established web developer and application programmer, with strong creative urges (which keep bursting out at the most inopportune moments) and a seemingly insatiable need to love all things animated. Living in the beautiful coastal town of Gordon's Bay in South Africa, he games, develops, takes in animated fare, trains under whichever martial arts dojo is closest at the time, and for the most part, simply enjoys life with his amazing wife and daughter.
Oh, and he draws ever now and then too.
This is a collection of things that he has managed to find the time to scribble down since 2007.
Looking for Something?
Jump to Category: