i am trying access values of textbox which is inside DIV using domXpath.
basically, i have multiple DIVs and each DIV contains some input type text and it has some values in it.
i want to create an array and map it with its parent DIV
my sample HTML code:
<div class="div_common_class">
<div class="content_area">
<div class="field_name">this is first</div>
</div>
<div class="input_area">
<input type="text" name="" value="111" />
</div>
</div>
<div class="div_common_class">
<div class="content_area">
<div class="field_name">this is second</div>
</div>
<div class="input_area">
<input type="text" name="" value="222" />
</div>
</div>
PHP code:
$dom = new DomDocument;
@$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
$result = $xpath->query('//div[contains(@class, "div_common_class")]');
from the above PHP code i only get the values of the DIV, but not getting values of input type text.
and output something like:
this is first 111
this is second 222
Your xpath gets the right divs, but you still need to get the values.
You could use getElementsByTagName
and use a foreach
or get the elements by item
like this example:
$results = $xpath->query('//div[contains(@class, "div_common_class")]');
foreach ($results as $result) {
echo sprintf("<b>%s %s</b><br>",
$result->getElementsByTagName("div")->item(1)->textContent,
$result->getElementsByTagName("input")->item(0)->getAttribute("value"));
}
That would give you:
this is first 111
this is second 222
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.