Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 27th, 2011, 08:35 PM
Registered User
 
Join Date: Jun 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default get array element from php

Hi.
I have an array on php, which is available to javascript:

document.getElementById("element_1").value = "<?php echo $arrdata[3][0]; ?>";


(noted the number 3 ? I need to put a javascript variable instead, 0,1,2 etc.)

I need this to be dynamic, so i have this javascript variable $curr added to dynamically construct the php part

var $mystr = '<' + "?" + "php" + " echo $arrdata[" +$curr+ '][0]; ?>';


Tried several ways:
document.getElementById("element_1").value = '"'+$mystr+'"';
or
document.getElementById("element_1").value = $mystr;

but still $mystr is not "expanded", so my control gets:
"<?php echo $arrdata[1][0]; ?>"
as a value, instead of the value from the array, which i insist, is available thrugh "<?php echo $arrdata[3][0]; ?>" for example.

How should I construct the value part to embed javascript $curr variable?

Any help greatly appreciated.

Carlos
 
Old June 28th, 2011, 04:33 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Carlos,

Not exactly sure what you're trying to accomplish, but it looks like you're mixing up server side PHP and client side JavaScript. The PHP code runs on the server first, generating HTML and JavaScript. This is then sent to the browser where the JavaScript executes. At that stage, you can't use the JavaScript variables in PHP as PHP has already finished generating the output.

If you can describe what it is you're doing, we may be able to suggest other solutions.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
check if an array element is not undefined crmpicco Javascript How-To 1 October 6th, 2005 11:40 AM
Array Element android66 Javascript 2 November 12th, 2004 04:50 AM
Passing php array values to javascript array gkrishna Pro PHP 0 November 6th, 2004 03:20 AM
How do I test for an empty array element kmoran Excel VBA 1 October 8th, 2004 03:34 AM
Removing an Element from an Associative Array nick8245 Javascript 2 September 26th, 2003 12:15 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.