473,503 Members | 1,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

php array in javascript function

i've got an array created in php that I want to send over to a javascript function.

onclick = "return sjekk(<?echo $names; ?> )"

in my "sjekk" function, when I try to print an alert :

function sjekk(navn)
{
alert(navn); // or alert(navn[0])
return(false);
}

I get "function Array { [native code]}"

How do I print out the names I want?
Jul 20 '05 #1
1 11655
Obscurr wrote:
i've got an array created in php that I want to send over to a javascript
function.

onclick = "return sjekk(<?echo $names; ?> )"
If $names is the PHP array, you need to convert it to a string,
using the print_r(...) and str_replace(...) PHP functions for
example (see PHP manual for details). Then you can pass that
string to the sjekk(...) JavaScript method while enclosing that
string in single quotes (since you used double quotes for the
event handler value already.) Quickhack:

onclick="return sjekk('<?php
/*
* Replace newlines in the output of print_r(...) with
* space because JavaScript string literals must end
* before newline.
*/
preg_replace("(\r?\n|\r)", ' ', print_r($names))
?>');"

But why do you pass the PHP array to JavaScript in the first place?
When you can use server-side PHP you should stick to it because it
is more reliable as client-side JavaScript can be disabled or not
supported.
in my "sjekk" function, when I try to print an alert :

function sjekk(navn) {
alert(navn); // or alert(navn[0])
return(false);
}
I get "function Array { [native code]}"


In PHP

$a = array(...);
echo $a;

prints

Array

which in

onclick = "return sjekk(<?echo $names; ?> )"

prints

onclick = "return sjekk(Array )"

But `Array' is a (constructor) function (for the Array prototype) in
JavaScript, so for the JavaScript engine you pass a reference to this
function to your method. When trying to display it with alert(...),
the toString(...) method of the Function prototype is called to convert
it to a String object and the source code of the method is displayed.
HTH

PointedEars

Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
55526
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
5
6500
by: Denis Perelyubskiy | last post by:
Hello, I need to make an array of elements accross forms. My javascript skills, as evident from this question, are rather rudimentary. I tried to make an associative array and index it with...
35
6589
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
2
5194
by: BrianP | last post by:
Hi, I have had to invent a work-around to get past what looks like a JavaScript bug, the malfunctioning Perl-like JavaScript array functions including SPLICE() and UNSHIFT(). I have boiled it...
38
5145
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
21
3168
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
4
9668
by: mflll | last post by:
I am looking into the different techniques of handling arrays of edit boxes in Java Script. The first program below works fine. However, are there better ways of doing this, where the person...
2
2480
by: JJA | last post by:
I'm looking at some code I do not understand: var icons = new Array(); icons = new GIcon(); icons.image = "somefilename.png"; I read this as an array of icons is being built. An element of...
10
3940
by: SM | last post by:
Hello I'm trying to create a multi dimensional array in JavaScript, but after some reading i still can't figure out how to apply it to my model. Here it is: I have a list A and for each item...
0
7188
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7063
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...
0
7258
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,...
1
6970
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...
0
5558
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,...
1
4987
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...
0
4663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1489
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 ...
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.