3

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!

2
  • 2
    Before moving to an alternative implementation, I would try asking the hosting provider if and when an upgrade to 5.2 is planned. Maybe politely ask whether it is at all possible to speed up the process Commented Sep 14, 2010 at 10:17
  • And json outside PHP binaries (e.g. in PHP script libraries) can be slow and buggy. Commented Nov 5, 2010 at 8:37

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

It seems that the Solar and Zend frameworks are the best, any advice on including them? I have NO idea on where to start.
1

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.

1 Comment

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?
0

See the PHP section:

http://json.org/

Comments

0

This is what you are looking for.

2 Comments

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)
-1

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.

1 Comment

more details on the "obscure setting" could prove helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.