Edit report at https://bugs.php.net/bug.php?id=61376&edit=1

 ID:                 61376
 User updated by:    jrbeaure at uvm dot edu
 Reported by:        jrbeaure at uvm dot edu
 Summary:            saveHTML only allows one option element to have the
                     selected attribute.
-Status:             Feedback
+Status:             Open
 Type:               Bug
 Package:            DOM XML related
 Operating System:   Linux (Unknown Derivative)
 PHP Version:        5.3.10
 Block user comment: N
 Private report:     N

 New Comment:

The problem only occurs with multiple select elements. It seems to work fine if 
I'm using the DOMElement::getElementsByTagName method, but I was able to 
reproduce it using the DOMXPath::query method.

<?php
$html =<<<EOF
<form>
  <select><option>foo</option><option>bar</option></select>
  <select><option>hello</option><option>PHP</option></select>
</form>
EOF;
$dom = new DOMDocument; $dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$selects = $xpath->query('//select');
foreach ($selects as $select) {
    $options = $xpath->query('//option', $select);
    foreach ($options as $option) $option->removeAttribute('selected');
    $options->item(1)->setAttribute('selected','selected');
}
echo $dom->saveHTML();
?>


Previous Comments:
------------------------------------------------------------------------
[2012-03-14 01:26:12] ahar...@php.net

Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.

This works fine for me:

<?php
$dom = new DOMDocument;
$dom->loadXML('<select><option>foo</option><option 
selected="selected">bar</option></select>');
foreach ($dom->getElementsByTagName('option') as $option) {
    $option->removeAttribute('selected');
    $option->setAttribute('selected', 'selected');
}
echo $dom->saveHTML();
?>

------------------------------------------------------------------------
[2012-03-13 15:35:39] jrbeaure at uvm dot edu

I forgot to mention in the description my work around.
I use saveXML instead, which works. However, this also breaks my code because 
the CDATA node markup in script tags cause the scripts to break in browsers, 
regardless of whether it's the official standard. To get around this I used 
preg_replace to surround the <![CDATA[]]> markup with javascript /*comment*/ 
markup.

------------------------------------------------------------------------
[2012-03-13 15:30:33] jrbeaure at uvm dot edu

Description:
------------
I've been having a very hard time trying to use the DOMDocument class for this 
purpose, and it's taken me three days to figure out how to work around the bugs.

When loading elements from different selects:

If there is a presently selected option, I remove the selected attribute from 
the option using the DOMElement removeAttribute method.

Then I use the DOMElement setAttribute method to set the attribute 'selected' 
to the value 'selected'.

I do this for two different options that are children of two different select 
elements.



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=61376&edit=1

Reply via email to