views:

141

answers:

6

Hi guys,

as the title specifies, my hosting provider does not have support for json_decode, so I need to find a way to adapt my code to achieve the same effect, but without using JSON, here is my code,

jQuery:

    var allLocations = [];

    $(".locations").each( function(i, location) {
        // for each location block
        location = $(location);
        var loc = {
            'province' : $("select[data-loc*='province']", location).val(),
            'town' : $("select[data-loc*='town']", location).val()
        };
        allLocations.push( loc );
    });

        //POST the locations information
        $.ajax({
                type: 'POST',
                url: 'locations.php',
                dataType: 'json',
                data: { locations: JSON.stringify(allLocations), uid: uid },
                success: function(data){
                    //alert(data)
                }
        });

PHP:

$json = $_POST['locations']; 
$uid = $_POST['uid']; // $json is a string
$json_array = json_decode($json, true); 

mysql_connect('localhost','user','pass') or die(mysql_error());
mysql_select_db('eskom_products') or die(mysql_error());

//insert the locations into the database
while($json_array as $key){
    $query = mysql_query("INSERT INTO suppliersLocations (supplier_id, province, town) VALUES('".$uid."', '".$key['province']."', '".$key['town']."' ) ") or die(mysql_error());
}

echo $text;

So as you can see, I am getting the province and town values of each location and creating a JSON object with it, which I then send off via $.ajax to a PHP file, but now since json_decode doesn't work, I need to try and find another way of fixing the problem, I was thinking of trying to pass an associative array to the php file, but I wanted to see what your guy's input would be, and if there might be a better way of achieving the desired result.

Thanx in advance!

+1  A: 

There is an alternative implementation of json_decode for PHP versions earlier than 5.2 (where json_* got included). It's called jsonwrapper and worked quite well for a project I did a while ago.

Alternatively have a look at some PEAR packages, e.g. Service_JSON.

halfdan
If I am not mistaken, it only adds the `json_encode` function, not the `json_decode` function which is what I really need, any alternatives?
Whoops, you're right. Have a look at Service_JSON then.
halfdan
+1  A: 

Hi,

you still can use JSON. There are several encoder/decoder libraries that word without the extension you mentioned. For example:

and others. Take a look at json.org

Timo
It seems that the Solar and Zend frameworks are the best, any advice on including them? I have NO idea on where to start.
There is a good example here: http://thecodecentral.com/2007/09/13/easy-json-encodingdecoding-in-php
Timo
A: 

See the PHP section:

http://json.org/

Camoy
A: 

This is what you are looking for.

Thariama
When I try that I get this "[malicious or incorrect JSON string]", but I am sending the same JSON string `json_decode` used, I am assuming that this class is not as robust.
well, i didn't encounter any problems, but there are many implementations out there who should fit (see the other answers)
Thariama
A: 

Seems that it was an obscure setting in the php config file that was messing with json_decode, once I disabled it, everything worked fine, Thanx for all of your help guys! I will edit this answer asap to describe how I got it working.