473,698 Members | 2,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to pass PHP array to javascript

11 New Member
I have a php array that consists of 4 string elements :
$arr = Array("A", "B", "C", "D");

i want to pass this array as an argument to a javascript function and alert the contents, how ? so i need something like this in javascript:
Expand|Select|Wrap|Line Numbers
  1. function x(my_arr) {
  2. for(i=0;i<my_arr.length;i++)
  3.   alert(my_arr[i]);
  4. }
I do not want a solution like this : function ('<?=$arr?>') { ... }

whenever I try to pass it, the alert function of javascript displays the word "ARRAY" ...
thanx in advance ..
Feb 13 '10 #1
4 3360
Dormilich
8,658 Recognized Expert Moderator Expert
obviously. if you print out a PHP array, that’s what you get. you have to print it in such a way, that it defines the array in JavaScript.
Expand|Select|Wrap|Line Numbers
  1. $str = "[";
  2. foreach ($arr as $val)
  3. {
  4.     $str += $val . ", ";
  5. }
  6. echo substr($str, 0, -1) . "];";
Feb 13 '10 #2
unpresedented
11 New Member
thank you so much for this cool idea ..
I appreciate it ...
Feb 13 '10 #3
Dormilich
8,658 Recognized Expert Moderator Expert
that’s plain JavaScript printed by PHP, nothing special.
Feb 13 '10 #4
xNephilimx
213 Recognized Expert New Member
you could also use json_encode function if you have php 5.2+

Expand|Select|Wrap|Line Numbers
  1. echo json_encode( $php_array );
  2.  
The output will be the same as dormilich method.
Be aware that in javascript there are no associative arrays like in php (arrays with string keys instead of numbers), so json_encode will encode such array as an object.
Feb 15 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
5951
by: Miguel | last post by:
Hello. I have a Javascript function that validates an specific form. As parameters this function receives an array of elements to be checked. Depending on the form in cause, the array could have variable lenght. I don't know how can i pass a dynamic array on 'onsubmit' event form PHP code to a JavaScript function. I would like to do something like: <script>
0
2449
by: Matt | last post by:
My problem is to allow ASP to interact with JSP, and I pass JavaScript object in my approach, but I wonder if it will work in network, not just in local machine. For testing purposes, the following are page1.html and page2.html that use Array JavaScript object to pass data back and forth. page1.html is able to transfer data to page2.html, but page2.html has trouble to transfer data back to page1.html.
3
6785
by: Nath | last post by:
Please help!? I am new to writing html, javascript, pretty new to MySQL but quite proficient at writing Perl and i'm a quick learner. I am building a database driven website and i am a little stuck: I have page of results obtained from a MySQL query presented as a table (the first column having checkboxes for each of the rows in the table, and all having the name "seqs"). I have set up a javascript (connected to a "toggle all" checkbox)...
5
3140
by: Seeker | last post by:
Newbie question here... I have a form with some radio buttons. To verify that at least one of the buttons was chosen I use the following code ("f" is my form object) : var btnChosen; for (count = 0; count <= 1; count++) { if (eval(f.RadioButtons.checked)) { btnChosen = true; }
4
13386
by: Sunny | last post by:
Hi, I am at present working on a jsp page which has a single text field. Upon entering a value in the field, i am trigerring an onChangeEvent() which calls a method of javascript. I am also dynamically creating an array in the jsp page and want to pass this array (by reference) to the javascript function. All I want to do in the javascript function is to check whether the input value matches with any of the array elements. However, i have...
2
6439
by: Augusto Cesar | last post by:
Hello people. How can I Pass ASP Array variable to Javascript; I´m to trying this: <script language="JavaScript"> var x = new Array(10);
2
1629
by: André | last post by:
Hi, I have to pass a lot of variables from vb.net to javascript using client callback. the problem is that there are single variables, but also arrays with differents indexes. I have two questions: 1) is this technology made for passing a lot of variables or in fact only for one? 2) does it exist a esier way to pass the values and to recover them in javascript?
1
5223
by: thomas_okken | last post by:
I'm working on a little C# DLL that will let me capture audio from within Internet Explorer. The idea is that the C# object, once it is recording, will periodically invoke a JavaScript callback, which will handle the audio somehow. The JavaScript callback is a simple function, which I pass to the C# object during its initialization; I then invoke the JavaScript callback using theCB.GetType().InvokeMember("", BindingFlags.InvokeMethod,...
4
4339
by: IRC | last post by:
hey, i am pretty new on javascript as well as PHP, Hey, anyone can you help me, how to pass the javascript array value to php page......... i want to retrieve the values which are arrayed on "selectedValues" from next page for php variable this is my javascript code saved on "sendValue.js" file, <script> function updateRecordEntry(requestValue){
1
1587
by: mchen1117 | last post by:
I try to display a random image alone with text and link, I find this javascript and did some edit as needed, I don't know how I can pass document.write() value to HREF's URL, Please help <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html dir="ltr" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Style-Type" content="text/css">...
0
8608
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9164
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8898
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7734
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6524
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3051
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.