|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2012-03-13 15:30 UTC] 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. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Thu Oct 30 23:00:01 2025 UTC | 
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(); ?>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(); ?>