/    Sign up×
Community /Pin to ProfileBookmark

Passing array from PHP to java and browse records

Hello,

I’ve got an array in PHP, with data like:

fieldvalue[0][1] = marijn
fieldvalue[0][2] = utrecht
fieldvalue[0][3] = 4 februari 1974

fieldvalue[1][1] = sinterklaas
fieldvalue[1][2] = madrid
fieldvalue[1][3] = 6 december 2008

etc…

I would like to sent the array ‘fieldvalue’ to a java variable (array).
Then I would like to create a function which responds to a [previous] and [next] button to browse trough the array, preferably without refreshing (ajax?).

Can you help me with that? or part of it?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@DokMar 29.2008 — Java is not JavaScript but here is how you can print/echo a JavaScript array
[CODE]print "
// Define array
fieldvalue = new Array();
// Define next dimension
fieldvalue[0] = new Array();
// Define elements
fieldvalue[0][0] = 'marijn';
fieldvalue[0][1] = 'utrecht';
fieldvalue[0][2] = '4 februari 1974';

fieldvalue[1] = new Array();
fieldvalue[1][0] = 'sinterklaas';
fieldvalue[1][1] = 'madrid';
fieldvalue[1][2] = '6 december 2008';
";[/CODE]
Copy linkTweet thisAlerts:
@marijn01authorApr 04.2008 — Putting a single variable into an array and reading the results work fine.

But how about passing the PHP variable, which is by then parsed to HTML, to a java(script) variable?

[code=php]$b=0;
while ($row = mysql_fetch_array($sql)) {
?><script language="JavaScript"><var fieldvalues[<? echo($b); ?>] = new Array();</script><?
for($a=0; $a<$num_fields; $a++) {
$fieldname = $name[$a];
// $fieldvalue[$b][$a] = $row[$fieldname];
?><script language="JavaScript">fieldvalues[<? echo($b); ?>][<? echo($a); ?>]="<? echo preg_replace("/r?n/", "\n", addslashes($row[$fieldname])); ?>";</script><?
}
$b++;
}[/code]

This example works with php and I do see the contents of the variables in the source of the page, something like:

[code=html]<script language="JavaScript">fieldvalues[0][0]="1";</script><script language="JavaScript">fieldvalues[0][1]="Gerald van Windt";</script><script language="JavaScript">fieldvalues[0][2]="Deze pionier van streetdance is dankzij zijn unieke, stoere dansstijl en creativiteit een van de .... Maar ook als je niet carrière gericht danst zijn Geralds lessen een erg goede training voor je conditie, houding en figuur en een garantie";</script>[/code]

But then when I call any of the, according to my info, defined variables, I get "undefined" on the place where I put the "document.write(fieldvalues[..][..]);" code.

I'm doing something very wrong here, I guess, but shouldn't I be able to pass variables which are processed on the server to javascript?
Copy linkTweet thisAlerts:
@DokApr 05.2008 — "Passing" a variable from PHP to JavaScript is essentially printing the entire JavaScript statement using PHP.

First you print your array to the HTML page as you would print any other HTML content. Don't use <script></script> for each loop - you only need it once and the language attribute is deprecated

PHP code for creating JavaScript array
[code=php]// This variable will contain the JavaScript code which you want to output
$js = "var fieldvalue = new Array();";
// Counter
$i =0;
// Get you data from database
while($row = mysql_fetch_array($result)) {
// Put each row into a JavaScript array
$js .= "fieldvalue[$i][0] = $row[var 1];";
$js .= "fieldvalue[$i][1] = $row[var 2];";
$js .= "fieldvalue[$i][2] = $row[var 3];";
$i++;
}
// Print the JavaScript to the HTML document
print "<script type="text/javascript">$js</script>";[/code]
×

Success!

Help @marijn01 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...