I'm trying to send an array to PHP from JS.
JS:
var json = JSON.stringify(extraFields);
url += "&json="+json;
PHP:
$json = json_decode($_GET['json'], true);
foreach($json as $K=>$V){
echo "json".$K . "=" . $V ."; ";
}
Assume extraFields
is a valid array in this format:
extraFields['key1'] = 'val1';
extraFields['key2'] = 'val2';
extraFields['key3'] = 'val3';
The PHP error I'm getting is invalid argument for Foreach
When I loop through the $_GET
values and just echo them, PHP shows empty brackets for $_GET['json']
so it's recognizing it as json..
What am I doing wrong?
Answer to TJ's comment
var extraFields = new Array();
var countFields = THIS.$_FIELDS.length;
var Row = new Array();
while(countFields--){
var name = THIS.$_FIELDS[countFields]['name'];
var id = THIS.$_FIELDS[countFields]['id'];
var elemVal = getElmVal(id);
extraFields[name] = elemVal;
window.alert(name +"="+ elemVal);
}
json_decode
, inspect whether json_decode returned null. If it did,echo json_last_error_msg();
to see what's wrong. – N.B. Mar 19 at 14:15extraFields
?= {}
?= []
? – T.J. Crowder Mar 19 at 14:16