php and DOM

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Indium
Forum Newbie
Posts: 15
Joined: Tue Jul 02, 2002 8:30 pm

php and DOM

Post by Indium »

Does php understand the DOM? Is it possible to drill down to a specific object like can be done with Javascript, e.g. how would the following javascript code be written in php.

Code: Select all

document.form.selectname.selectedindex.value
Can anybody point me to some links for that kind of stuff?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

first of all: please read this sticky thread

document.form.selectname.selectedindex.value is a client-side 'runtime-value' that php will probably never see.
Indium
Forum Newbie
Posts: 15
Joined: Tue Jul 02, 2002 8:30 pm

Post by Indium »

Ok, so php doesn't understand javascript. I was under the impression php could be used to get around the cross-browser issues of javascript and the issue of javascript being disabled by the client. If you can't access a form object and it's properties using php then how can php help with these issues?
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

I was under the impression that php could be used to get around the cross-browser issues of javascript ...
Yes, this is true. It (I should say, "The Developer") does this by sending a script to a browser based on what that browser is.
If you can't access a form object and it's properties using php then how can php help with these issues?
Is it the object you need, or the data returned when the submit button is pressed? That makes a big difference. Explain why you would need the object so that we can begin to understand and possibly help.

Later on,
BDKR
Indium
Forum Newbie
Posts: 15
Joined: Tue Jul 02, 2002 8:30 pm

Post by Indium »

What I had in mind was two dropdown menu's. The the selection of the first menu would dynamically generate the selections for the second dropdown menu. The values of the selections from each menu would be submitted to a script that would generate the url of the appropriate page.

I could do this in javascript but would like to do this in php. The only reasons for doing this in php is to learn the language and to try to circumvent the problems of using a javascript. I guess I am trying to fit the problem to php rather than match php to the problem, just so I can learn php.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Well, you could do it with PHP, but with each selection, the form and it's info needs to make a trip back to the server. This is far less than ideal and the user won't like you much for it either.

That siad, Javascript makes a lot of sense along these lines. Below is the function that I use.

Code: Select all

function fillSelectFromArray(selectCtrl, itemArray, goodPrompt, badPrompt, defaultItem) 
    {
    var i, j;
    var prompt;
    // empty existing items
    for (i = selectCtrl.options.length; i >= 0; i--) 
	{
	selectCtrl.optionsїi] = null; 
	}
    prompt = (itemArray != null) ? goodPrompt : badPrompt;
    if (prompt == null) 
	{
	j = 0;
	}
    else 
	{
	selectCtrl.optionsї0] = new Option(prompt);
	j = 1;
	}

    if (itemArray != null) 
	{
        // add new items
	for (i = 0; i < itemArray.length; i++) 
	    &#123;
	    selectCtrl.options&#1111;j] = new Option(itemArray&#1111;i]&#1111;0]);
	    if (itemArray&#1111;i]&#1111;1] != null) 
		&#123;
		selectCtrl.options&#1111;j].value = itemArray&#1111;i]&#1111;1];
		&#125;
	    j++;
	    &#125;
	// select first item (prompt) for sub list
	selectCtrl.options&#1111;0].selected = true;
	&#125;
    &#125;
Pick something in one drop down and it populates another drop down based on that choice. If you need help with it, let me know.

Later on,
BDKR
Indium
Forum Newbie
Posts: 15
Joined: Tue Jul 02, 2002 8:30 pm

Thanks

Post by Indium »

Thanks, I'll give it a try.
Post Reply