Viewed   199 times

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.

 Answers

5

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

?>
Thursday, August 4, 2022
 
4

The right code to get a div with class is:

$ret = $html->find('div.foo');
//OR
$ret = $html->find('div[class=foo]');

Basically you can get elements as you were using a CSS selector.

source: http://simplehtmldom.sourceforge.net/manual.htm
How to find HTML elements? section, tab Advanced

Wednesday, December 14, 2022
 
5

Do you guys know a BB parser class written in PHP that I can use for this, instead of regexes?

There's the BBCode PECL extension, but you'd need to compile it.

There's also PEAR's HTML_BBCodeParser, though I can't vouch for how effective it is.

There are also a few elsewhere, but I think they're all pretty rigid.

I don't believe that either of those do what you're looking for, with regard to having a callback for tag contents (and then @webarto is totally correct in that HTMLPurifier is the right tool to use when processing the contents). You might have to write your own here. I've previously written about my experiences doing the same that you might find helpful.

Thursday, November 17, 2022
 
1

In your example code, you have

echo $x = $html->find('h2[class="section-heading"]',1)->outertext; 

as you are calling find() with a second parameter of 1, this will only return the 1 element. If instead you find all of them - you can do whatever you need with them...

$list = $html->find('h2[class="section-heading"]');
foreach ( $list as $item ) {
    echo $item->outertext . PHP_EOL;
}

The full code I've just tested is...

include(__DIR__."/simple_html_dom.php");
$html = file_get_html('http://campaignstudio.in/');

$list = $html->find('h2[class="section-heading"]');
foreach ( $list as $item ) {
    echo $item->outertext . PHP_EOL;
}

which gives the output...

<h2 class="section-heading text-white">We've got what you need!</h2>
<h2 class="section-heading">At Your Service</h2>
<h2 class="section-heading">Let's Get In Touch!</h2>
Saturday, August 27, 2022
 
serg3z
 
5

For example, I have the following HTML, where all the words are lowercase:

<div>
    <h2>page not found!</h2>
    <p>go to <a href="/">home page</a> or use the <a href="/search">search</a>.</p>
</div>

My task is to convert text to capitalized words. To solve it, I fetch all text nodes and convert them using the ucwords function (of course, you should use your translation function instead of it).

libxml_use_internal_errors(true);
$dom = new DomDocument();
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);

foreach ($xpath->query('//text()') as $text) {
    if (trim($text->nodeValue)) {
        $text->nodeValue = ucwords($text->nodeValue);
    }
}

echo $dom->saveHTML();

The above outputs the following:

<div>
    <h2>Page Not Found!</h2>
    <p>Go To <a href="/">Home Page</a> Or Use The <a href="/search">Search</a>.</p>
</div>
Tuesday, October 25, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :