Retrieve attribute value of hidden input element using DOMXPath

I have a piece of HTML code:

<form method="post" action="/"> <input type="hidden" name="example-name" value="example-value"> <button type="submit">Submit</button> </form> 

How can I extract hidden input value using DOMXPath in PHP? I tried something like this:

 //$site - the html code $doc = new DOMDocument(); $doc->loadHTML($site); $xpath = new DOMXpath($doc); $kod = $xpath->query("//input[@name='example-name']"); foreach($kod as $node) $values[]=$node->nodeValue; return $values; 

But it returns an empty array. Where is the mistake?

+4
source share
2 answers

Try this to get the value attribute of the input element with the name example-name attribute

 '//input[@name="example-name"]/@value' 

Result

 Array ( [0] => example-value ) 

Your XPath did not select the attribute axis (I think it named), but the text axis, and since the input has no text, the value in the array was empty. He found the item though.

+7
source
 $node->getAttribute('value'); 
+3
source

Source: https://habr.com/ru/post/1300520/


All Articles