Text array transfer from JS to PHP procedure

Questions related to customising nuBuilder Forte with Javascript or PHP.

Text array transfer from JS to PHP procedure

Postby Janusz » Fri May 03, 2019 4:50 pm

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?
Janusz
 
Posts: 162
Joined: Fri Dec 28, 2018 10:11 pm
Location: Krakow, Poland

Re: Text array transfer from JS to PHP procedure

Postby kev1n » Fri May 03, 2019 5:24 pm

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));
kev1n
 
Posts: 183
Joined: Mon Oct 15, 2018 2:13 am

Re: Text array transfer from JS to PHP procedure

Postby Janusz » Fri May 03, 2019 6:30 pm

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
Janusz
 
Posts: 162
Joined: Fri Dec 28, 2018 10:11 pm
Location: Krakow, Poland

Re: Text array transfer from JS to PHP procedure

Postby admin » Fri May 10, 2019 5:44 am

.
admin
Site Admin
 
Posts: 3067
Joined: Mon Jun 15, 2009 9:53 am


Return to Custom Code