tags:

views:

76

answers:

2

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?

+3  A: 

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.

Gordon
+1  A: 
$node->getAttribute('value');
useless