Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Text array transfer from JS to PHP procedure

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
Janusz
nuBuilder Team
Posts: 506
Joined: Fri Dec 28, 2018 1:41 pm
Location: Krakow, Poland
Has thanked: 8 times
Been thanked: 18 times

Text array transfer from JS to PHP procedure

Unread post 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?
If you like nuBuilder, please leave a review on SourceForge
kev1n
nuBuilder Team
Posts: 4305
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 446 times
Contact:

Re: Text array transfer from JS to PHP procedure

Unread post 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));
Janusz
nuBuilder Team
Posts: 506
Joined: Fri Dec 28, 2018 1:41 pm
Location: Krakow, Poland
Has thanked: 8 times
Been thanked: 18 times

Re: Text array transfer from JS to PHP procedure

Unread post 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
If you like nuBuilder, please leave a review on SourceForge
admin
Site Admin
Posts: 2815
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Text array transfer from JS to PHP procedure

Unread post by admin »

.
Post Reply