p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > PHP/MySQL > Beginning PHP
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.

Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 19th, 2007, 01:28 AM
Registered User
Points: 14, Level: 1
Points: 14, Level: 1 Points: 14, Level: 1 Points: 14, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to MooMan65 Send a message via AIM to MooMan65
Default Javascript + PHP Issue

Hey everyone! I'm having an issue with reading PHP variables from Javascript which should be loaded on the client-side already. What I have done is:

1) Populate a PHP array on load from data in a mySQL database.
2) Populate a HTML form combo box with elements.

Basically, each element in the combo box, when clicked, populates a SECOND combo box with elements taken from the PHP array. So like:

Blue -> Sky, Water, Blueberries.
Green -> Grass, Grapes

(this is an example lol, not what I'm actually doing)

The first combo box has Blue and Green. And Sky, Water, Blueberries, Grass and Grapes are stored in the mySQL database. The PHP code reads them from the database and sorts them out, placing them in the array with each index holding the elements belonging to a different colour (in the form of "Sky|Water|Blueberries" and "Grass|Grapes").

The elements in the first combo box all have the value of the ID that the MySQL database uses to identify which secondary items belong to it (say Blue is 2, and Green is 5). So effectively I then have an array like this:

Code:
$colors[2] = "Sky|Water|Blueberries"
$colors[5] = "Grass|Grapes"
Now, in Javascript, I read the value of the currently selected item in the first combo box through an onChange event:

Code:
var id = document.colors.options[document.colors.selectedIndex].value;
And then attempt to read the appropriate string from the PHP variable through:

Code:
var colorNames = "<?php echo ($colors["+id+"]) ?>";
Except it doesn't seem to like my inserting that Javascript variable 'id' in there. The ID variable does indeed return the selected value, and you can read from the $colors if you hardcode an index in there e.g.:

Code:
var colorNames = "<?php echo ($colors[2]) ?>";
But not dynamically using the ID variable. Is it actually possible to do this? Have I done something stupid elsewhere? Am I missing the plot completely? Thanks for any input you can provide. :)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old June 22nd, 2007, 06:47 AM
Friend of Wrox
 
Join Date: Nov 2005
Location: Rotterdam, , Netherlands.
Posts: 223
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, Let me guess you want to click on the first combo box for instance you click blue. then you want the values "blue, sea , blueberries: to apear in the seccond combo box right?

There are to things you can do you have to transfer every php array to a javascript array to make this possible but that wouldnt be the most efficient.

The second way and probably the way you should do it is Use AJAX. find some tutorials and articles about AJAX.

__________________________________________________ ________
I am DJ Kat...that's my name. Its a D and a J and a Kat with a K.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old June 24th, 2007, 06:02 PM
Registered User
Points: 14, Level: 1
Points: 14, Level: 1 Points: 14, Level: 1 Points: 14, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to MooMan65 Send a message via AIM to MooMan65
Default

Thanks for the reply. :) The trick of mine is that the data I want in the combo boxes are from a database; am I correct in assuming AJAX will allow me to interface with PHP in the way that this isn't allowing, or with the database directly?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old June 25th, 2007, 06:34 AM
Friend of Wrox
 
Join Date: Nov 2005
Location: Rotterdam, , Netherlands.
Posts: 223
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't understand your question but let me give a try. Using ajax(which is a combination of javascript using the XMLHTTPRequest object and a serverside script PHP in your case) allows you to get database data and transfer this to javascript variables without refreshing the page.  Instead only a part of your webpage will be updated by javascript(in your case the select box)

check out the following link for a database ajax example
http://www.w3schools.com/ajax/ajax_database.asp

__________________________________________________ ________
I am DJ Kat...that's my name. Its a D and a J and a Kat with a K.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
IIS - javascript issue TE32 Internet Information Services 0 October 13th, 2008 03:53 PM
postback issue with drop downlists with javascript emachines Javascript 3 January 24th, 2005 06:54 AM
postback issue with drop downlists with javascript emachines Javascript How-To 1 January 6th, 2005 01:49 PM
PHP/Design issue wolftrap1 BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 4 February 4th, 2004 09:53 PM
Tricky Javascript/XSL issue...Hmmmm robster XSLT 1 January 23rd, 2004 11:28 AM



All times are GMT -4. The time now is 04:05 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc