skip to Main Content

I have a form that contains multiple Select
I want to retrieve the getAttribute('value') and textContent of the <option> elements
to build an array

I have a code that works under:
PHP Versions 5.6.34, 7.2.34, 7.4.21

Does not work under:
PHP Versions 7.0.33, 7.4.33

Example of my code:



</form>
<select id="imageextention" name="imageextention" onchange="viewimage('formsig');" class="select">
<option value="blue.gif">sigblue</option>
<option value="green.gif">siggreen</option>
<option value="orange.gif">sigorange</option>
</select>

<select id="fontcolor" name="fontcolor" onchange="viewimage('formsig');">
<option value="#7BCEDE">blue</option>
<option value="#A5B531">green</option>
<option value="#FF8C29">orange</option>
<option value="#F76B7B">pink</option>
<option value="#B584BD">purple</option>
<option value="#FFBD08">yellow</option>
</select>
</form>
   $tagname = $doc->getElementById('imageextention')->nodeName;
   if ($tagname == "select") {
      $serials = $doc->getElementById('imageextention')->childNodes;
      $imageextentionM = array();
      foreach ($serials as $n) {
           array_push($imageextentionM[$n->getAttribute('value')] = $n->textContent);
      }
   }

   $tagname2 = $doc->getElementById('fontcolor')->nodeName;
   if ($tagname2 == "select") {
      $serials = $doc->getElementById('fontcolor')->childNodes;
      $fontcolorM = array();
      foreach ($serials as $n) {
         array_push($fontcolorM[$n->getAttribute('value')] = $n->textContent);
      }
      $activecolortxtM="";
   }
}

with PHP Versions 7.0.33, 7.4.33 I have this error:

Call to undefined method DOMText::getAttribute() …… Stack trace: #0 {main} thrown in ….

It does not recognize getAttribute('value') for child elements

I know my code is not clean.

I tried with getElementsByTagName but it lists me all "option" elements of the page

$num = $doc->getElementById("imageextention")->childNodes->length;
$items = $doc->getElementsByTagName('option');
for ($i=0; $i < $num; $i++) {
   $items = $doc->getElementsByTagName('option');
   $imageextentionM[$items[$i]->getAttribute('value')]= $items[$i]->textContent;
}

Thank you in advance for your assistance.

2

Answers


  1. Chosen as BEST ANSWER

    Great ! It works, thank you very much @CBore

    My code :

    
      $tagname2 = $doc->getElementById('fontcolor')->nodeName;
      if ($tagname2 == "select") {
         $serials = $doc->getElementById("imageextention")->getElementsByTagName('option');
         $fontcolorM = array();
         foreach ($serials as $n) {
            array_push($fontcolorM[$n->getAttribute('value')] = $n->textContent);
         }
         $activecolortxtM="";
      }
    }
    

  2. Just guessing, but you expect somewhere to get the DOMElement object but you get DOMText object instead (or in addition) which do not have method getAttribute, most likely at ->childNodes which also contains the text nodes. So check for the type would solve the error:

    foreach ($serials as $n) {
        if($n instanceof DOMElement){
            array_push($imageextentionM[$n->getAttribute('value')] = $n->textContent);
        }
        // to investigate which object you got instead:
        /* else { 
            var_dump($n);
        }*/
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search