DOM TextArea (and dom chart please)

DOM TextArea (and dom chart please)

am 02.02.2010 20:29:08 von PHP Gen

Hey!
i'm just starting with PHP's DOM-XML and need a little help please.


Basically, first i am trying to see if a input like a textbox has a 'VALUE=' associated with it, if yes, i leave it be, if no, i add a default value.

This *is working* as can be seen by the attached code below.

But a bit confused as to how to do the same for a textarea as textarea's do not have a 'VALUE=' attribute.

Heres my code:


$website_data = file_get_contents('dom_test.html');//load the website data,

$dom = new DomDocument; //make a new DOM container in PHP
$dom->loadHTML($website_data); //load all the fetched data into the DOM container

$inputs = $dom->getElementsByTagName('input'); // Find Sections

foreach ($inputs as $input) { //*** this block has the guts of the functionality ***
if(!$input->getAttribute("value") || $input->getAttribute("value")=="") {
$input->setAttribute("value", "RRRRRRR");
}
}



// ***** now we come to the textarea bit that is not working *****

$inputs2 = $dom->getElementsByTagName('textarea'); // Find textareas

foreach ($inputs2 as $input2) {
if(!$input2->firstChild.nodeValue=="") {
$input2->firstChild.nodeValue=="it works!";
}
}



echo $dom->saveHTML();
?>

+++++++++++++++++++++++++++++++

// I have even tried this instead of the above:

foreach ($inputs2 as $input2) {
if(!$input2->getAttribute("defaultValue")=="") {
$input2->setAttribute("defaultValue","it works!");
}
}

but no joy.

I'm betting its pretty simple but i dont have a "DOM chart" for php, the only ones i have been able to find via google are for javascript like this one: http://www.hscripts.com/tutorials/javascript/dom/textarea-ev ents.php


and that chart does not help much. Does anyone have a PHP DOM chart or a resource that i can use to get started using this?

Thanks in advance!
Ryan



------
- The faulty interface lies between the chair and the keyboard.
- Creativity is great, but plagiarism is faster!
- Smile, everyone loves a moron. :-)





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: DOM TextArea (and dom chart please)

am 02.02.2010 20:35:26 von Ashley Sheridan

--=-KfNaQbYE40XnzHNpFQE2
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Tue, 2010-02-02 at 11:29 -0800, Ryan S wrote:

> Hey!
> i'm just starting with PHP's DOM-XML and need a little help please.
>
>
> Basically, first i am trying to see if a input like a textbox has a 'VALUE=' associated with it, if yes, i leave it be, if no, i add a default value.
>
> This *is working* as can be seen by the attached code below.
>
> But a bit confused as to how to do the same for a textarea as textarea's do not have a 'VALUE=' attribute.
>
> Heres my code:
>
>
> $website_data = file_get_contents('dom_test.html');//load the website data,
>
> $dom = new DomDocument; //make a new DOM container in PHP
> $dom->loadHTML($website_data); //load all the fetched data into the DOM container
>
> $inputs = $dom->getElementsByTagName('input'); // Find Sections
>
> foreach ($inputs as $input) { //*** this block has the guts of the functionality ***
> if(!$input->getAttribute("value") || $input->getAttribute("value")=="") {
> $input->setAttribute("value", "RRRRRRR");
> }
> }
>
>
>
> // ***** now we come to the textarea bit that is not working *****
>
> $inputs2 = $dom->getElementsByTagName('textarea'); // Find textareas
>
> foreach ($inputs2 as $input2) {
> if(!$input2->firstChild.nodeValue=="") {
> $input2->firstChild.nodeValue=="it works!";
> }
> }
>
>
>
> echo $dom->saveHTML();
> ?>
>
> +++++++++++++++++++++++++++++++
>
> // I have even tried this instead of the above:
>
> foreach ($inputs2 as $input2) {
> if(!$input2->getAttribute("defaultValue")=="") {
> $input2->setAttribute("defaultValue","it works!");
> }
> }
>
> but no joy.
>
> I'm betting its pretty simple but i dont have a "DOM chart" for php, the only ones i have been able to find via google are for javascript like this one: http://www.hscripts.com/tutorials/javascript/dom/textarea-ev ents.php
>
>
> and that chart does not help much. Does anyone have a PHP DOM chart or a resource that i can use to get started using this?
>
> Thanks in advance!
> Ryan
>
>
>
> ------
> - The faulty interface lies between the chair and the keyboard.
> - Creativity is great, but plagiarism is faster!
> - Smile, everyone loves a moron. :-)
>
>
>
>
>


When I get stuck on things like this, I find print_r() invaluable as it
can basically dump out the objects entire contents, so I can see what
values it has set.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-KfNaQbYE40XnzHNpFQE2--

Re: DOM TextArea (and dom chart please)

am 02.02.2010 21:06:47 von PHP Gen

--0-1108063210-1265141207=:92616
Content-Type: text/plain; charset=us-ascii




>// I have even tried this instead of the above:
>
>foreach ($inputs2 as $input2) {
> if(!$input2->getAttribute("defaultValue")=="") {
> $input2->setAttribute("defaultValue","it works!");
> }
>}
>
>but no joy.
>
>I'm betting its pretty simple but i dont have a "DOM chart" for php, the only ones i have been able to find via google are for javascript like this one: http://www.hscripts.com/tutorials/javascript/dom/textarea-ev ents.php
>
>
>and that chart does not help much. Does anyone have a PHP DOM chart or a resource that i can use to get started using this?
>
>
When I get stuck on things like this, I find print_r() invaluable as it can basically dump out the objects entire contents, so I can see what values it has set.
====================

Hey Ash,
Thanks for replyng.

Where exactly do i use the print_r? and on which variable?
Because i have tried it in different places but still no luck..



--0-1108063210-1265141207=:92616--

Re: DOM TextArea (and dom chart please)

am 02.02.2010 21:12:41 von Michael Peters

Ryan S wrote:

>
> $website_data = file_get_contents('dom_test.html');//load the website data,
>
> $dom = new DomDocument; //make a new DOM container in PHP
> $dom->loadHTML($website_data); //load all the fetched data into the DOM container

I'm not sure what the answer to your issue is, but mind if I make a
couple off topic recommondations?

1) Use loadXML() instead of loadHTML()

The reason is that loadHTML() will mutilate multibyte utf8 characters,
replacing them with entities.

You can still use $dom->saveHTML() to present the data if html is your
target output.

2) loadXML() is less forgiving of malformed content, but you can fix
that by using tidy to import your data

$website_data = new tidy('dom_test.html',$tidy_config);
$website_data->cleanRepair();
$dom->loadXML($website_data);

where

$tidy_config is the tidy configuration array.
Make sure you set

$tidy_config['output-xhtml'] = true;

so that the output of tidy is clean X(ht)ML for loadXML().

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: DOM TextArea (and dom chart please)

am 03.02.2010 05:18:09 von Andrew Ballard

On Tue, Feb 2, 2010 at 2:29 PM, Ryan S wrote:
> Hey!
> i'm just starting with PHP's DOM-XML and need a little help please.
>
>
> Basically, first i am trying to see if a input like a textbox has a 'VALU=
E=3D' associated with it, if yes, i leave it be, if no, i add a default val=
ue.
>
> This *is working* as can be seen by the attached code below.
>
> But a bit confused as to how to do the same for a textarea as textarea's =
do not have a 'VALUE=3D' attribute.
>
> Heres my code:
>
>
> $website_data =3D file_get_contents('dom_test.html');//load the website d=
ata,
>
> $dom =3D new DomDocument; //make a new DOM container in PHP
> $dom->loadHTML($website_data);  //load all the fetched data into the=
DOM container
>
[snip]
> // ***** now we come to the textarea bit that is not working *****
>
> $inputs2 =3D $dom->getElementsByTagName('textarea'); // Find textareas
>
> foreach ($inputs2 as $input2) {
>        if(!$input2->firstChild.nodeValue=="") {
>                $input2->firstChil=
d.nodeValue=="it works!";
>        }
> }
>
>
>
> echo $dom->saveHTML();
> ?>
>
> +++++++++++++++++++++++++++++++
>
> // I have even tried this instead of the above:
>
> foreach ($inputs2 as $input2) {
>        if(!$input2->getAttribute("defaultValue")=3D=
=3D"") {
>                $input2->setAttrib=
ute("defaultValue","it works!");
>        }
> }
>
> but no joy.
>
> I'm betting its pretty simple but i dont have a "DOM chart" for php, the =
only ones i have been able to find via google are for javascript like this =
one: http://www.hscripts.com/tutorials/javascript/dom/textarea-ev ents.php
>
>
> and that chart does not help much.  Does anyone have a PHP DOM chart=
or a resource that i can use to get started using this?
>
> Thanks in advance!
> Ryan
>

I think what you are looking for is $input2->textContent in PHP.

Andrew

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: DOM TextArea (and dom chart please)

am 03.02.2010 11:34:00 von Michael Peters

Michael A. Peters wrote:

>
> $website_data = new tidy('dom_test.html',$tidy_config);

Doh!

Should be

$website_data = new tidy('dom_test.html',$tidy_config,'utf8');

Otherwise it has the same problem with multibyte characters that
loadHTML() has. But with the 'utf8' specified it works beautifully.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: DOM TextArea (and dom chart please)

am 03.02.2010 17:53:23 von PHP Gen

> I think what you are looking for is $input2->textContent in PHP.

Hey Andrew (and everyone else was was kind enough to write back) !

Found the solution, this is what i am using (and it works!), and i hope it helps anyone else who finds themselves in the spot i found myself

$inputs2 = $dom->getElementsByTagName('textarea'); // Find textareas
foreach ($inputs2 as $input2) {
if(!$input2->nodeValue || $input2->nodeValue=="") {
$input2->nodeValue="it works!";
}
}


Cheers guys!
/R





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php