In JavaScript in innerHTML I can enter tags. In PHP DOM it i

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
sneta
Forum Newbie
Posts: 5
Joined: Wed Nov 11, 2009 4:47 am

In JavaScript in innerHTML I can enter tags. In PHP DOM it i

Post by sneta »

s xlated to entitites and entered as text. Is there a similiar mechanism in PHP? If not what do I do if I want to enter designed code? (by "designed" I mean with tags like <b><i> etc)
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: In JavaScript in innerHTML I can enter tags. In PHP DOM

Post by McInfo »

PHP Manual: What is PHP?

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 2:04 pm, edited 1 time in total.
sneta
Forum Newbie
Posts: 5
Joined: Wed Nov 11, 2009 4:47 am

In PHP5+ in PHP DOM

Post by sneta »

Can I insert html tags into text tags that won't be replaced into plain text? In JavaScript there's innerHTML which allows that. Does PHP have something similar?
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: In JavaScript in innerHTML I can enter tags. In PHP DOM it i

Post by flying_circus »

What McInfo is trying to communicate is that PHP doesnt have a DOM as you are asking. PHP cannot interact with rendered HTML code, as PHP is processed on the server BEFORE any HTML is sent to a browser and rendered.

You can use PHP to inject "designed code" but after it is sent to the client, PHP is done.

As for the link that McInfo posted, he is saying "RTFM to better understand exactly what PHP IS and DOES", because you havent got a grasp on that concept yet.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: In JavaScript in innerHTML I can enter tags. In PHP DOM

Post by McInfo »

It is possible to work with an HTML document using DOM...

Code: Select all

<?php
$html = new DOMDocument();
$html->loadHTML('<html><head><title></title></head><body></body></html>');
 
$html->getElementsByTagName('title')->item(0)->appendChild($html->createTextNode('Page Title'));
 
$p = $html->createElement('p', 'Paragraph in the Body');
$html->getElementsByTagName('body')->item(0)->appendChild($p);
 
echo $html->saveHTML();
...but the usual way is to create HTML is to simply output it directly.

Code: Select all

<html>
    <head>
        <title><?php echo 'Page Title'; ?></title>
    </head>
    <body>
        <p><?php echo 'Paragraph in the Body'; ?></p>
    </body>
</html>
sneta wrote:Can I insert html tags into text tags that won't be replaced into plain text?
I'm not sure what that means. Can you give an example?

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 2:05 pm, edited 1 time in total.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: In JavaScript in innerHTML I can enter tags. In PHP DOM it i

Post by flying_circus »

McInfo wrote:It is possible to work with an HTML document using DOM...
Whoops, its apparent I misunderstood what he was asking. :oops:
sneta
Forum Newbie
Posts: 5
Joined: Wed Nov 11, 2009 4:47 am

Using PHP DOMDoc,DOMElement etc is EXACTLY what I mean. Howe

Post by sneta »

When I create say a <div> and I want to enter <b>bold</b> the problem is that "<" is xlated to entity "<" while i want to enter a real tag. An additional complication is that I get the html to "inject" into the div from outside so I can't know that in advance and I have to add to HTML as is.
sneta
Forum Newbie
Posts: 5
Joined: Wed Nov 11, 2009 4:47 am

$d = $html->createElement('div', '<b>bold</b>');

Post by sneta »

won't work. the tags are xformed to entities "<" turns to "<" or plain text. To make things more complicated I get the HTML that I have to enter the div from outside so I can't know in advance what it is and I have to put it all in. A simple javascript innerHTML is working in client side but how do I solve it in server side?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: In JavaScript in innerHTML I can enter tags. In PHP DOM

Post by McInfo »

Import the HTML into the current DOMDocument with DOMDocument::createDocumentFragment() and DOMDocumentFragment::appendXML(), then use DOMNode::appendChild() to place the fragment where you need it.

Edit: This post was recovered from search engine cache.
Post Reply