|
|
 |
| 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.
|
 |

June 19th, 2007, 01:28 AM
|
|
Registered User
|
|
Join Date: Nov 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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. :)
|

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
|
|
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.
|

June 24th, 2007, 06:02 PM
|
|
Registered User
|
|
Join Date: Nov 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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?
|

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
|
|
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.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |