Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i have to encode an array of email addresses using javascript into json string and send to abc.php using ajax. in abc.php i have to decode it and send emails to all the address into that array.

currently i'm encoding the array into json using

var json_string = JSON.stringify(myarray);

in abc.php i am decoding it using

$emails = json_decode($_POST['json_string']);
// json_string was passed as POST variable using ajax

but it gives NULL when printed..

how can i decode it and access individual emails in the php file

share|improve this question
 
What does var_dump($_POST['json_string']) give you, or var_dump($_POST) for that matter? –  deceze Dec 23 '12 at 8:20
 
print_r gives [\"[email protected]\",\"[email protected]\"] and var_dump (json_decode($_POST['json_string'])) gives null –  vinique Dec 23 '12 at 8:22
6  
Looks like magic quotes? –  deceze Dec 23 '12 at 8:24
 
It may be helpful to see your actual ajax call. It may be a datatype thing. –  Joshua Enfield Dec 23 '12 at 8:41
 
Yeah, could be magic quotes. Try unescaping the string before decoding. –  Artem Goutsoul Dec 23 '12 at 9:27
show 3 more comments

1 Answer

If you have access to the php.ini of your webserver, the best thing would be to disable magic_quotes at all, because they are deprecated:

; Magic quotes
;

; Magic quotes for incoming GET/POST/Cookie data.
magic_quotes_gpc = Off

; Magic quotes for runtime-generated data, e.g. data from SQL, from exec(), etc.
magic_quotes_runtime = Off

; Use Sybase-style magic quotes (escape ' with '' instead of \').
magic_quotes_sybase = Off

If you don't have server access, use a .htaccess file with the following option

php_flag magic_quotes_gpc Off

If you don't want to use that, the last thing that remains is using an unescape function such as

function ref_stripslashes(&$value,$key) {
    $value = stripslashes($value);
}

if((function_exists("get_magic_quotes_gpc") && get_magic_quotes_gpc()) || (ini_get('magic_quotes_sybase') && (strtolower(ini_get('magic_quotes_sybase'))!="off")) ) {
    array_walk_recursive($_GET,'ref_stripslashes');
    array_walk_recursive($_POST,'ref_stripslashes');
    array_walk_recursive($_COOKIE,'ref_stripslashes');
}

This was taken from the php manual, Lucifer's comment

json_decode($_POST['json_string']) should then work.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.