PHP json_decode not supported, any alternatives?

as indicated in the header, my hosting provider does not have json_decode support, 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 get the values ​​for the provinces and cities of each location and create a JSON object with it, which I then send through $.ajaxto the PHP file, but now since it’s json_decodenot working, I need to try to find another way to solve the problem, I I was thinking of trying to pass an associative array to a php file, but I wanted to see what would happen to your boyfriend, and if there could be a better way to achieve the desired result.

Thanx in advance!

+3
5

, php, json_decode, , , Thanx ! , , .

0

json_decode PHP 5.2 ( json_ *). jsonwrapper , .

PEAR, . Service_JSON.

+1
+1
source

See the PHP section:

http://json.org/

0
source

This is what you are looking for.

0
source

Source: https://habr.com/ru/post/1764653/


All Articles