PHP and Document Object Model

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
cdub93
Forum Newbie
Posts: 2
Joined: Thu Nov 09, 2006 11:21 am

PHP and Document Object Model

Post by cdub93 »

I'm wondering if PHP supports access to the Document Object Model?

For example ...

Can I implement the same or similar statement below using PHP?

Client-side Javascript :

Code: Select all

document.getElementById("textInFile").innerHTML
Thanks,
Chris
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

php is a server-side language therefore it has access to the DOM only in so far as it sends it to the client. which translates to: not really at all.

if you want to do anything with the DOM, you really need to use client side scripting.

you can however use things like ajax to call things from the server-side and 'inject' them into the client side.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

*cough* Read the DOM part of the documentation:

http://www.php.net/dom
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

that is the first I've ever heard of those functions. I could see how they would be useful for XML type documents, but how/why else would you use them? Given that PHP is server side, I have a hard time understanding why you'd need access to the DOM to serve up pages. I could potentially see some benefit with things like file_get_contents() to help parse out the string, and make changes to served pages, but beyond that, how can you have interactivity with the DOM with php?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Burrito wrote:that is the first I've ever heard of those functions. I could see how they would be useful for XML type documents, but how/why else would you use them? Given that PHP is server side, I have a hard time understanding why you'd need access to the DOM to serve up pages. I could potentially see some benefit with things like file_get_contents() to help parse out the string, and make changes to served pages, but beyond that, how can you have interactivity with the DOM with php?
Ohh... the possibilities are endless if you throw DOM into a template/MVC system :)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

d11wtq wrote:Ohh... the possibilities are endless if you throw DOM into a template/MVC system :)
sorry for my ignorance, but I'd love for you to help an old man out and give me an example :D
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Same here.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Burrito wrote:
d11wtq wrote:Ohh... the possibilities are endless if you throw DOM into a template/MVC system :)
sorry for my ignorance, but I'd love for you to help an old man out and give me an example :D
I don't have one. I wasn't implying I've done it, I was saying it would be nice. I have thought about it in the past because my template stuff already does things like this. This is oibviously a completely stupid example but it's much like cut and paste... but with parts of a page.

Code: Select all

<?php

class View
{
    // ... snip ...
    
    public fuction blahBlahBlahAndErmmBlah()
    {
        $node = $this->template()->copy("my_tag");
        $block = $this->template()->cut("some_block"); //Can also be used to delete
        $this->template()->replace("foo", $node);
        $this->template()->append($block);
    }
}
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

again, excuse the ignorance, but how would doing something like that be advantageous to just writing out the html? (I mean using the library of functions to create / remove / etc elements in the DOM, not using MVC or any other design pattern.) I'm having a hard time wrapping my head around the advantages to using these functions. The object model makes perfect sense to me on pages that are loaded (to be able to manipulate them after they've been pulled down), but I just can't seem to grasp the benefit of using them server side.

a scenario like this perhaps???

A CMS system that uses a templating engine. A user wants to create a form and needs to add elements to the form. They select the elements to add, submit the page, and based on the selections made, the form gets built by these functions? I can sort of see some potential there, but still seems like using a sledge hammer to push in a thumbtack.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

It just gives me more control over the features on the template (delete and edit buttons for admin users maybe?) from within the view rather than directly placing such logic in the template. I use this to set CSS/JavaScript into the <head> of a larger page at runtime.

Your example is a reasonably good one anyway. The controller picks up what the user wants to do and the view then makes those changes on the template. Using other means you'd need more PHP code directly in the template itself, which is fine if it's minimal logic but for scarier, more complex logic I just like this approach. Maybe it's hackish, I've never actually had anybody else review what I'm doing :)
Post Reply