vendredi 8 janvier 2021

How to determine if a checkbox is checked?

On a website it has a checkbox HTML code like this:

<input type="checkbox" name="mycheckbox" id="mycheckbox" value="123" checked="checked">

How do I check if the checkbox is already checked via DomDocument and the xPath? I essentially just want a boolean telling me True if it is checked. I am unsure how to get this though.

<?php

$dom = new DOMDocument();
$content = file_get_content('https://example.com');
@$dom->loadHtml($content);

// Xpath to the checkbox
$xp = new DOMXPath($dom);
$xpath = '//*[@id="mycheckbox"]'; // xPath to the checkbox
$answer = $xp->evaluate('string(' . $xpath . ')');



Aucun commentaire:

Enregistrer un commentaire