0
<html>

<head>
<?PHP
include('simple_html_dom.php');
?>
<title>

</title>
</head>

<body>
<form name ="form1" method ="POST" ACTION="parser.php">
<input type="text" name="parser1" style="height:200px; width:200pt;"></br></br>
<input type="submit" value="Submit"></br></br>
</form>

<?php

$html_str = $_POST['parser1'];

// Create DOM from URL or file
$html = file_get_html($html_str);
$html->load('
<form name="form1" action="parser.php" method="post">
<input type="text" name="parser1">
</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>';
?>
</body>

</html>

Here i am trying to enter the html source into the text box parser1

I am then catching the data from the textbox using post into a string html_str

when i try to parse that string, i start getting errors.

Fatal error: Call to a member function load() on a non-object in /home/public_html/parser.php on line 24

please help

2 Answers 2

1

You have this:

$html = file_get_html($html_str);
$html->load('
<form name="form1" action="parser.php" method="post">
<input type="text" name="parser1">
</form>');

The error message says that $html is not an object. file_get_html() isn't a builtin function but you appear to be using PHP Simple HTML DOM Parser. Its API documentation says that it returns an object but fails to provide additional info. If we look at the source code:

function file_get_html($url, $use_include_path = false, $context=null, $offset = -1, $maxLen=-1, $lowercase = true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)
{
    // We DO force the tags to be terminated.
    $dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText);
    // For sourceforge users: uncomment the next line and comment the retreive_url_contents line 2 lines down if it is not already done.
    $contents = file_get_contents($url, $use_include_path, $context, $offset);
    // Paperg - use our own mechanism for getting the contents as we want to control the timeout.
    //$contents = retrieve_url_contents($url);
    if (empty($contents) || strlen($contents) > MAX_FILE_SIZE)
    {
        return false;
    }
    // The second parameter can force the selectors to all be lowercase.
    $dom->load($contents, $lowercase, $stripRN);
    return $dom;
}

... we can see it returns FALSE some times:

if (empty($contents) || strlen($contents) > MAX_FILE_SIZE)
{
    return false;
}

So I'd dare say that your POST field is either empty or too large. You should really check that before calling ->load().

Update:

The file_get_html() function:

Creates a DOM object from a file or a URL.

I guess what you really want is str_get_html():

Creates a DOM object from a string.

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

Comments

0

It might help if you actually check if the form is submitted or not. You have to check and re-check if the input is valid or not.

// check if it's a POST request
if($_SERVER['REQUEST_METHOD'] === 'POST') {
    // check if parser1 is not empty
    if(!empty($_POST['parser1'])) {
        $input = $_POST['parser1'];

        if(filter_var($input, FILTER_VALIDATE_URL)) { // looks like an URL
            $html = file_get_html($input); // download URL
        } else { // lets assume it's HTML, because it's not an URL
            $html = str_get_html($input);
        }

        // If something goes wrong here, the input is invalid
        if(!empty($html)) {
             // parse DOM document here
        } else {
            // There is something wrong with the input
        }
    }
}

4 Comments

no, the $html seems to be not working. How do i solve that part of the problem? I need the user to input the source of the web page into the text box and then i will extract data from that source code.
yes, the document is not being submitted... how do i go around this and accomplish my target?
@debal check out my answer again.
i am getting this new warning Warning: file_get_contents(here is the data that i insert into the text box) [function.file-get-contents]: failed to open stream: No such file or directory in /home/public_html/simple_html_dom.php on line 75

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.