Grabbing hidden inputs as a string (Using PHP Simp

2019-02-13 15:02发布

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 form action which is (get_me.php). What I'm looking to do now is grab the 2 hidden inputs, but not the values. I want to grab the inputs themselves.

E.G: Here's my form:

<form action="get_me.php" method="post">
    <input type="text" name="get_me_two">
    <input type="text" name="get_me_three">
    <input type="hidden" name="meta_required" value="from">
    <input type="hidden" name="meta_forward_vars" value="0">
</form>

And what I want to grab from here is the two hidden inputs, Not the values, the complete string.

I'm not sure how to grab these using: PHP Simple HTML DOM Parser, if anybody knows a way that would be great, if not, if there's an alternative that also would be great. Once I've grabbed these I plan on passing the 2 input values to another page with the hidden strings, and of course the form action.

Also, if anybody is interested here's my full code, which includes the simple html dom functionality.

<?php

include("simple_html_dom.php");

// Create DOM from URL or file
$html = file_get_html('form_show.php');
$html->load('
<form action="get_me.php" method="post">
<input type="text" name="get_me_two">
<input type="text" name="get_me_three">
<input type="hidden" name="meta_required" value="from">
<input type="hidden" name="meta_forward_vars" value="0">
</form>');

// Get the form action
foreach($html->find('form') as $element) 
   echo $element->action . '<br>';

// Get the input name       
foreach($html->find('input') as $element) 
   echo $element->name . '<br>';
?>

So, the end result would grab the 3 values, and then the 2 hidden inputs (full strings). Help would be much appreciated as It's driving me a little mad trying to get this done.

2条回答
我想做一个坏孩纸
2楼-- · 2019-02-13 15:18

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);

?>
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-02-13 15:23

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.

查看更多
登录 后发表回答