Hi guys,
I am trying to parse a JSON string using PHP, the JSON gets sent to the PHP file using jQuery $.ajax
in this format, [{"value":"59"},{"value":"7"},{"value":"46"}]
, but for some odd reason I keep getting this error "Invalid argument supplied for foreach()"
, here is my PHP and jQuery code,
jQuery:
$(".systems").each( function(i, system) {
// for each location block
system = $(system);
var sys = {
'value' : $("select[data-prod='products']", system).val()
};
allSystems.push( sys );
});
$.ajax({
type: 'POST',
url: 'systems.php',
dataType: 'json',
data: { systems: JSON.stringify(allSystems), uid: uid },
success: function(data){
alert(data)
}
});
PHP:
require_once 'classes/Zend/Json.php';
$json = $_POST['systems'];
$uid = $_POST['uid'];
$array= Zend_Json::decode("$json");
mysql_connect('localhost','user','pass') or die(mysql_error());
mysql_select_db('products') or die(mysql_error());
//insert the suppliers products into the database
foreach($array as $key){
$query = mysql_query("INSERT INTO suppliersProducts (product_id, supplier_id) VALUES('".$key['value']."', '".$uid."' ) ") or die(mysql_error());
}
print_r($json);
As you can see I am using the Zend framework's JSON decode function to decode the JSON string that gets passed to the PHP, and the format seems correct to me so I have no idea what else it could be that is causing this error, maybe encoding? Any ideas?
Thanx in advance!