Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
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 Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 19th, 2007, 12:28 AM
Registered User
 
Join Date: Nov 2004
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. :)

 
Old June 22nd, 2007, 05:47 AM
Friend of Wrox
 
Join Date: Nov 2005
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.
 
Old June 24th, 2007, 05:02 PM
Registered User
 
Join Date: Nov 2004
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?

 
Old June 25th, 2007, 05:34 AM
Friend of Wrox
 
Join Date: Nov 2005
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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
IIS - javascript issue TE32 Internet Information Services 0 October 13th, 2008 02: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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.