FRIHOSTFORUMSSEARCHFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Arrays?

 


coreymanshack
So I'm passing an array to my javascript via php via ajax.

Here is the array that's been passed in.

PHP:
Code:
Array (     [0] => Array         (             [ConsultantID] => 2             [ConsultantIN] => GAW             [Login] => GAW002             [Consultant] => Gene Williams             [SecurityLevel] => 1             [Extension] => 0             [UserName] => gwilliams             [Password] => blah             [Address] => 123 Blah Ln             [City] => Fort Worth             [State] => TX             [ZipCode] => 76108             [Email] => blah@blah.com         )  )


when i access

Javascript:
Code:
array[0]['Consultant']


it says undefined.


EDIT: I accidentaly put a dollar sign in front of array[0]['Consultant'] when I orignally wrote this effectivley making it php code but it was actually javascript.

what have I done wrong?
How can i export an array from php to javascript better?


Last edited by coreymanshack on Sun Sep 27, 2009 5:07 pm; edited 2 times in total
Marcuzzo
Code:
<?php
$array = array ( "0" => array ( "ConsultantID" => 2,  "ConsultantIN" => "GAW" , "Login" => "GAW002",  "Consultant" => "Gene Williams" , "SecurityLevel" => 1 , "Extension" => 0 , "UserName" => "gwilliams",  "Password" => blah , "Address" => "123 Blah Ln" , "City" => "Fort Worth" , "State" => "TX",  "ZipCode" => 76108 , "Email" => "blah@blah.com" ) ) ;

echo $array[0]["Consultant"];

?>



http://be2.php.net/manual/en/language.types.array.php
coreymanshack
Marcuzzo wrote:
Code:
<?php
$array = array ( "0" => array ( "ConsultantID" => 2,  "ConsultantIN" => "GAW" , "Login" => "GAW002",  "Consultant" => "Gene Williams" , "SecurityLevel" => 1 , "Extension" => 0 , "UserName" => "gwilliams",  "Password" => blah , "Address" => "123 Blah Ln" , "City" => "Fort Worth" , "State" => "TX",  "ZipCode" => 76108 , "Email" => "blah@blah.com" ) ) ;

echo $array[0]["Consultant"];

?>



http://be2.php.net/manual/en/language.types.array.php


That didn't help at all.
Aredon
Have you tried just echoing the php on the page instead of javascript to make sure that it's defined? Try echoing parts of the array on the page as raw text, or even do a print_r of the whole array to make sure all entries are defined. If they are, then it's a problem with your javascript. I would also make sure that you have single quotes around each key string.

(Assuming I have the order right... Ajax -> Php -> JS?)
[OOC: I believe this is in the wrong board as well :O]
leontius
You said that you're passing an array from php to javascript but I don't see javascript at all. $array[0]['Consultant'] is definitely a PHP construct. What ajax method/framework do you use?

I also find firebug (a firefox extension) to be very helpful in developing ajax apps. In particular you can take a peek of any data transferred through xmlhttprequest in firebug.
coreymanshack
leontius wrote:
You said that you're passing an array from php to javascript but I don't see javascript at all. $array[0]['Consultant'] is definitely a PHP construct. What ajax method/framework do you use?

I also find firebug (a firefox extension) to be very helpful in developing ajax apps. In particular you can take a peek of any data transferred through xmlhttprequest in firebug.


I'm not using a framework, I read I need to serialize the array in php and use a php unserialize function in javascript.

Aredon wrote:
Have you tried just echoing the php on the page instead of javascript to make sure that it's defined? Try echoing parts of the array on the page as raw text, or even do a print_r of the whole array to make sure all entries are defined. If they are, then it's a problem with your javascript. I would also make sure that you have single quotes around each key string.

(Assuming I have the order right... Ajax -> Php -> JS?)
[OOC: I believe this is in the wrong board as well :O]


I echoed the php via print_r already and I get the desired array.

As far as I know ajax is just javascript using data that is usually retrieved using the XMLHttpRequest object.
leontius
coreymanshack wrote:
leontius wrote:
You said that you're passing an array from php to javascript but I don't see javascript at all. $array[0]['Consultant'] is definitely a PHP construct. What ajax method/framework do you use?

I also find firebug (a firefox extension) to be very helpful in developing ajax apps. In particular you can take a peek of any data transferred through xmlhttprequest in firebug.


I'm not using a framework, I read I need to serialize the array in php and use a php unserialize function in javascript.

Aredon wrote:
Have you tried just echoing the php on the page instead of javascript to make sure that it's defined? Try echoing parts of the array on the page as raw text, or even do a print_r of the whole array to make sure all entries are defined. If they are, then it's a problem with your javascript. I would also make sure that you have single quotes around each key string.

(Assuming I have the order right... Ajax -> Php -> JS?)
[OOC: I believe this is in the wrong board as well :O]


I echoed the php via print_r already and I get the desired array.

As far as I know ajax is just javascript using data that is usually retrieved using the XMLHttpRequest object.


https://developer.mozilla.org/en/AJAX/Getting_Started
http://www.xul.fr/en-xml-ajax.html
http://www.xul.fr/ajax-javascript-json.html
Aredon
coreymanshack wrote:
leontius wrote:
You said that you're passing an array from php to javascript but I don't see javascript at all. $array[0]['Consultant'] is definitely a PHP construct. What ajax method/framework do you use?

I also find firebug (a firefox extension) to be very helpful in developing ajax apps. In particular you can take a peek of any data transferred through xmlhttprequest in firebug.


I'm not using a framework, I read I need to serialize the array in php and use a php unserialize function in javascript.

Aredon wrote:
Have you tried just echoing the php on the page instead of javascript to make sure that it's defined? Try echoing parts of the array on the page as raw text, or even do a print_r of the whole array to make sure all entries are defined. If they are, then it's a problem with your javascript. I would also make sure that you have single quotes around each key string.

(Assuming I have the order right... Ajax -> Php -> JS?)
[OOC: I believe this is in the wrong board as well :O]


I echoed the php via print_r already and I get the desired array.

As far as I know ajax is just javascript using data that is usually retrieved using the XMLHttpRequest object.

Make sure all your strings are in single quotes. I saw some discrepancy there between your array and your echo. (the keys)
coreymanshack
Aredon wrote:
coreymanshack wrote:
leontius wrote:
You said that you're passing an array from php to javascript but I don't see javascript at all. $array[0]['Consultant'] is definitely a PHP construct. What ajax method/framework do you use?

I also find firebug (a firefox extension) to be very helpful in developing ajax apps. In particular you can take a peek of any data transferred through xmlhttprequest in firebug.


I'm not using a framework, I read I need to serialize the array in php and use a php unserialize function in javascript.

Aredon wrote:
Have you tried just echoing the php on the page instead of javascript to make sure that it's defined? Try echoing parts of the array on the page as raw text, or even do a print_r of the whole array to make sure all entries are defined. If they are, then it's a problem with your javascript. I would also make sure that you have single quotes around each key string.

(Assuming I have the order right... Ajax -> Php -> JS?)
[OOC: I believe this is in the wrong board as well :O]


I echoed the php via print_r already and I get the desired array.

As far as I know ajax is just javascript using data that is usually retrieved using the XMLHttpRequest object.

Make sure all your strings are in single quotes. I saw some discrepancy there between your array and your echo. (the keys)


If I serlize the arrayi shouldn't have any problems with my array.
coreymanshack
leontius wrote:
coreymanshack wrote:
leontius wrote:
You said that you're passing an array from php to javascript but I don't see javascript at all. $array[0]['Consultant'] is definitely a PHP construct. What ajax method/framework do you use?

I also find firebug (a firefox extension) to be very helpful in developing ajax apps. In particular you can take a peek of any data transferred through xmlhttprequest in firebug.


I'm not using a framework, I read I need to serialize the array in php and use a php unserialize function in javascript.

Aredon wrote:
Have you tried just echoing the php on the page instead of javascript to make sure that it's defined? Try echoing parts of the array on the page as raw text, or even do a print_r of the whole array to make sure all entries are defined. If they are, then it's a problem with your javascript. I would also make sure that you have single quotes around each key string.

(Assuming I have the order right... Ajax -> Php -> JS?)
[OOC: I believe this is in the wrong board as well :O]


I echoed the php via print_r already and I get the desired array.

As far as I know ajax is just javascript using data that is usually retrieved using the XMLHttpRequest object.


https://developer.mozilla.org/en/AJAX/Getting_Started
http://www.xul.fr/en-xml-ajax.html
http://www.xul.fr/ajax-javascript-json.html


So you are saying I should be using XML or JSON to xfer data from php to ajax instead of just echoing plain text? Smile

EDIT: Thank you, JSON is AWESOME!!
Related topics

Multidimensional Arrays & passing info between windows
Visual Basic Tutorial - Control Arrays: What, Why and How
PHP page from arrays displays nothing, what's going on?
Is it safe to edit arrays?
Php Arrays :((((((((

Inserting PHP multidimensional arrays into a mySQL database
PHP Write to line
[php scripts ] phpweather&email
How To : Improve Your PHP Programming
Interview: Derek Liu, Gaia Online Anime Community

gmail drive
Tutorial: PHP Installed Modules Dynamic Reference Tool
Programming
Programming links, info, and tutorials
Why You Should Never Use Flash

Nutch
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.