Encode json from javascript and decode using php

I need to encode an array of email addresses using javascript in a json string and send abc.php using ajax. in abc.php I need to decode it and send emails to the whole address in this array.

I am currently encoding an array in json using

var json_string = JSON.stringify(myarray); 

in abc.php i decode it with

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

but when printed it gives NULL.

how can I decode it and access individual letters in php file

+4
source share
1 answer

If you have access to php.ini of your web server, it is best to disable magic_quotes because they are deprecated:

 ; Magic quotes ; ; Magic quotes for incoming GET/POST/Cookie data. magic_quotes_gpc = Off ; Magic quotes for runtime-generated data, eg 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 do not have access to the server, use the .htaccess file with the following option

 php_flag magic_quotes_gpc Off 

If you do not want to use this, the last thing that remains is to use a 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 directory, Lucifer comment

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

+2
source

All Articles