Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How do I get the value of an input field like the one below where it does not have an ID attribute using PHP's DOMDocument?

<input type="text" name="make" value="Toyota">
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
824 views
Welcome To Ask or Share your Answers For Others

1 Answer

XPath makes it simple, assuming that's the only text input with "make" as its name:

$dom = new DOMDocument();
$dom->loadHTML(...);
$xp = new DOMXpath($dom);
$nodes = $xp->query('//input[@name="make"]');
$node = $nodes->item(0);

$car_make = $node->getAttribute('value');

If there's more than one input with that particular field name on the page (which is entirely possible), then you'll have to do some extra work to narrow down WHICH of those multiple inputs you want.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...