php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61376 saveHTML only allows one option element to have the selected attribute.
Submitted: 2012-03-13 15:30 UTC Modified: 2013-12-03 09:30 UTC
From: jrbeaure at uvm dot edu Assigned:
Status: Not a bug Package: DOM XML related
PHP Version: 5.3.10 OS: Linux (Unknown Derivative)
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jrbeaure at uvm dot edu
New email:
PHP Version: OS:

 

 [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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-03-13 15:35 UTC] 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-14 01:26 UTC] aharvey@php.net
-Status: Open +Status: Feedback
 [2012-03-14 01:26 UTC] aharvey@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-14 15:10 UTC] jrbeaure at uvm dot edu
-Status: Feedback +Status: Open
 [2012-03-14 15:10 UTC] jrbeaure at uvm dot edu
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();
?>
 [2013-12-03 09:30 UTC] mike@php.net
-Status: Open +Status: Not a bug
 [2013-12-03 09:30 UTC] mike@php.net
Your XPATH query overrides the context node: //options always fetches the all options, not those from the current select.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 06 04:01:32 2024 UTC