Page 1 of 1

Text array transfer from JS to PHP procedure

PostPosted: Fri May 03, 2019 4:50 pm
by Janusz
Hi,
I want to pass text array from JS script to PHP procedure.
For example to send: red , green, blue, black to PHP as array
So in JavaScript I am trying to create:
Code: Select all
var a = 'array("red", "green", "blue","black");' ;
nuSetProperty('HIST_UPDATE',a);
nuRunPHPHidden('HIST_UPDATE', 1);

and in PHP - HIST_UPDATE proc.
Code: Select all
$x='#HIST_UPDATE#';
nuDebug($x);
nuDebug($x[1]);
...

with following result from debug
Code: Select all
[0] : array(\"red\", \"green\", \"blue\",\"black\");
[0] : r

With different trials I always have "\" before " or '.
Do you have some suggestion how to tranfer text array to PHP?

Re: Text array transfer from JS to PHP procedure

PostPosted: Fri May 03, 2019 5:24 pm
by kev1n
Try JSON.stringify() to convert the array to string and json_decode to decode with PHP:

JS:

Code: Select all
var colors = ["red","green","blue","black"];
var json = JSON.stringify(colors);
nuSetProperty('HIST_UPDATE',json);
nuRunPHPHidden('HIST_UPDATE', 1);


PHP:

Code: Select all
$x = "#HIST_UPDATE#";
$j = json_decode($x);
// or this: $j = json_decode( html_entity_decode( stripslashes ($x));

Re: Text array transfer from JS to PHP procedure

PostPosted: Fri May 03, 2019 6:30 pm
by Janusz
Hi Kev1n,
Thanks for your support. The second case is working.
$j = json_decode( html_entity_decode( stripslashes ($x)));
plus explode() to turn into array

Re: Text array transfer from JS to PHP procedure

PostPosted: Fri May 10, 2019 5:44 am
by admin
.