JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old April 30th, 2004, 02:10 PM
marysolm80 marysolm80 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 2 marysolm80 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy Array in javascript from php

Please i have this code to use and php array in a javascript function
<script language="JavaScript" type="text/JavaScript">
function cargar()
{

var xx=new Array()
xx[0]="<?php echo $vector[0]; ?>"

alert(xx[0]);
}
</script>

and in the body tag
<?php
$vector[0]=1;
$vector[1]=2;
?>

The Cargar() function I call it from a botton.
I got and error that says "string constant without end" or
something lie that, at this line
xx[0]="<?php echo $vector[0]; ?>"
I tried to put ; at the end of the line, bot nathinsg seems to work

Please help me, i just need to know how to use a php array in javascrit.
Thanks..

Reply With Quote
  #2  
Old May 1st, 2004, 12:58 PM
marysolm80 marysolm80 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 2 marysolm80 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I got it!!!

Well, is me again!, The problem I had was the ubication of the javascript in my page,
so I put it at the end and it works!!!
But Now I have a new problem, the position of the array that i want to use in javascript
is a variable and i dont know how to use it. I put like this, but it doesn`t work.

var p=0;
xx[0]="<?php echo $vector["+p+"]; ?>"

why???? please help me!!!

Reply With Quote
  #3  
Old May 3rd, 2004, 04:40 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 4 m 48 sec
Reputation Power: 8
What is it you're t rying to accomplish by combining the PHP and JAvascript this way?
sometimes it gets complicated, and there's usually an easier way.

Your problem above is because you're expecting PHP to understand the JAvascript variable... but remember that Javascript is called on the client side AFTER the PHP has already rendered...

If you want to send a PHP array to a Javascript array, try this:


PHP Code:
<?php $array = array('one''two''three'); ?>

<script language="JavaScript" type="text/JavaScript">

var xx=new Array("<?=implode("\",\""$array); ?>")

</script>


The above code is off the top of my head. It will hopefully lead you to some suplimentary answers.

Reference:

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > Array in javascript from php


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Iron Speed




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway