7

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 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Got the full hidden inputs using (thanks): // Get the input hiddens foreach($html->find('input[type=hidden]') as $element) echo $element; Now I'm just wondering what's the best way to store these individually and pass them to another page. Is there a way to store these in a variable, each input hidden?.
Store them in an array: $hidden = array('meta_required' => xxx, 'meta_forward_vars => yyy); and use that to rebuild the hidden inputs when it comes time to build the new form. Or just save the find() results somewhere and use that as your source instead of a new array.
Hmm, while considering I want to pass this information to another page for my form that is already pre-styled, is it best to store this information in a session of some sort? Not quiet sure how I'd store the elements individually in a session.
the session array's an array like any other that just happens to get persisted between requests. $_SESSION['hidden values'] = array(...).
Ended up getting it to work with your guidance, appreciate the help, thanks.
2

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

?>

1 Comment

DOMDocument::saveHTML() expects exactly 0 parameters getting this as warning

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.