I have a got 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 value of the hidden input using DOMXPath in PHP? I have tried somethig 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?
Try this to get the value
attribute of the input
element with the name
attribute example-name
'//input[@name="example-name"]/@value'
Result
Array
(
[0] => example-value
)
Your XPath didn't select the attribute axis (I think that's what it's called) but the text axis and since input has no text, the value in the array was empty. It did find the element though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With