Pass JavaScript value to PHP

Information for PHP. Hypertext Preprocessor
Post Reply
dzcadii
Posts: 94
Joined: Sun Mar 01, 2015 4:36 am

Pass JavaScript value to PHP

Post by dzcadii » Fri Jun 12, 2015 3:20 am

So, here is a little way to pass a variable, array, or object from JavaScript to PHP.

JavaScript side:

Code: Select all

var encData = encodeURIComponent(JSON.stringify(<what you want to pass to the back end>));
//Typically I put this into a hidden form input but you can do what you need. Like: $('#selectorID').val(encData)
//any way you do this it should be passed with $_POST (preferred) or $_GET (like in an ajax call etc.)
PHP side:

Code: Select all

$encData = json_decode(rawurldecode($_POST['encData']));
//Keep in mind if you need an array make sure you force the array with true like:
$encData = json_decode(rawurldecode($_POST['encData']), true);

Post Reply