Forum Moderators: coopster

Message Too Old, No Replies

PHP Dom and input boxes

<input /> tags are not valid xml?

         

RyanM

4:57 am on Oct 30, 2006 (gmt 0)

10+ Year Member



Hi,

I have created a class to control my forms, form validation, post backs, etc using the PHP5 DOM. Ok so my code basically is:


$el = $this->doc->getElementByID($id);
if($el->nodeName == "input") {
$el->setAttribute("value", $value);
}else { //any normal html item
$el->nodeValue = "";
$elString = "<" . $el->nodeName;
$elString2 = "</" . $el->nodeName . ">";
if ($el->hasAttributes()){
foreach ($el->attributes as $attribute){
$elString .= " " . $attribute->name . "='" . $attribute->value ."'";
}
}
$elString .= ">";
$toolbar= new DOMDocument(); //PHP 5 DOM
$toolbar->loadHTML( $elString . $value . $elString2);
//echo $toolbar->saveHTML();
$node = $this->doc->importNode($toolbar->getElementsByTagName($el->nodeName)->item(0),true);
$test = $el->parentNode->replaceChild($node, $el);
} //fi

A bit messy I know, anyway if the nodename is input then I simply ammend the attribute within the node, otherwise I rebuild the entire node (is there a better way of doing this?)

Anyway.

This code works fine on my test machine but the minute I load it up into a live environment it fails to find any nodes which are inputs, it just ignores them, if I change those input fields to textareas then the code works fine. I have read that Input tags are not supported by the php5 DOM, is this so? If so thats a big oversite on their behalf, if not whats wrong with the live server?

Thanks

eelixduppy

11:33 pm on Nov 1, 2006 (gmt 0)



Hello,

Have you read any of the user contributed notes for DOM XML Functions [us2.php.net]? You may find your answer there.

Sorry for my poor reponse. Just wanted to give you something :)

StupidScript

12:29 am on Nov 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you say that you change the input fields to textareas and it works, are you changing both the actual form elements and your code (
if($el->nodeName == "textarea") {
), or just the form element types?

string DomNode->node_name ( void )

Returns name of the node. The name has different meanings for the different types of nodes as illustrated in the following table.

From the manual [us3.php.net]. (Sorry, PHP DOM stuff is pretty new ...)

RyanM

1:11 am on Nov 2, 2006 (gmt 0)

10+ Year Member



Hi guys.

I suppose I should have been more specific. Im using the PHP5 DOM rather than Dom XML.

The main problem that im having is that Dom XML's function GetElementByID ignores input boxes.

If I create a simple page with an input box with an id of input1 and then do:

$el = $this->doc->getElementByID("input1"); el returns false.

If I change the input box into a textarea $el returns the correct node.

RyanM

4:24 am on Nov 2, 2006 (gmt 0)

10+ Year Member



Ok on research it seems like this is a bug within PHP 5.1 and has been fixed within 5.2. Unfortunately I cant find PHP 5.2 within in any Debian package repositories, and I dont want to recompile PHP on a live server, so its messy hack time.