Grabbing hidden inputs as a string (Using PHP Simple HTML DOM Parser)

后端 未结 2 1525
不知归路
不知归路 2021-01-05 10:28

So I have a form that has 4 inputs, 2 text, 2 hidden. I\'ve grabbed the two text input values from the name, which are (get_me_two, get_me_three) and I\'ve also grabbed the

相关标签:
2条回答
  • 2021-01-05 10:41

    I don't use the SimpleDom (I always go whole-hog and use DOMDocument), but couldn't you do something like ->find('input[@type=hidden]')?

    If the SimpleDOM doesn't allow that sort of selector, you could simply loop over the ->find('input') results and pick out the hidden ones by comparing the attributes yourself.

    0 讨论(0)
  • 2021-01-05 10:47

    If you use DomDocument, you could do the following:

    <?php
        $hidden_inputs = array();
        $dom = new DOMDocument('1.0');
        @$dom->loadHTMLFile('form_show.php');
    
        // 1. get all inputs
        $nodes = $dom->getElementsByTagName('input');
    
        // 2. loop through elements
        foreach($nodes as $node) {
            if($node->hasAttributes()) {
                foreach($node->attributes as $attribute) {
                    if($attribute->nodeName == 'type' && $attribute->nodeValue == 'hidden') {
                        $hidden_inputs[] = $node;
                    }
                }
            }
        } unset($node);
    
        // 3. loop through hidden inputs and print HTML
        foreach($hidden_inputs as $node) {
            echo "<pre>" . htmlspecialchars($dom->saveHTML($node)) . "</pre>";
        } unset($node);
    
    ?>
    
    0 讨论(0)
提交回复
热议问题