Jump to content

Help regarding user input


ggenc

Recommended Posts

Hey guys! I'm pretty new to php and starting work on a project, the first part I'm working on is a html page with text fields like this:

 

URL:

Dropdown box:

 

Submit

 

when the submit happens I want it to post to a php page, parse whatever is inside a set of <h2> tags and print it to to the php page, also the user selects four options from the dropdown box and I need to print the string contained in the selection to the next page as well, As I said I'm pretty new but here's what i threw together so far based on references I found, but it doesn't work correctly:

 

parse.html:

 

<html>
<body>

<form action="parse.php" method="post">
URL: <input type="text" name="turl" /> 
<br>
Option:
<select name="selectskill">
        <option value="1">Low</option>
        <option value="2">Mid</option>
        <option value="3">High</option>
        <option value="4">Known</option>
    </select>
    <br>
<br>
<input type="submit" />
</form>

</body>
</html> 

 

parse.php:

 

<html>
<head></head>
<body>
<?php
   $lol = $_POST["turl"];
   $str = file_get_contents($lol);
   $DOM = new DOMDocument;
   $DOM->loadHTML($str);
   // Grab text inside the heading 2 tags
   $items = $DOM->getElementsByTagName('h2');
   echo "option: " . $lol;
   // Display the content inside the second set of H2 tags
   echo $items->item(1)->nodeValue . "<br/>";
   		
?>
</body>
</head>
</html>

 

Thanks for all the help in advance!

Link to comment
Share on other sites

well basically there is a title in the page I'm trying to parse (title of a tournament) and it is between a set of <h2> tags, I was testing it on a test tournament page and the title of the tournament was "Test tourney" now when I input the link to a new tournament it just keeps saying "test tourney", as if it's not parsing the new link. also the option from the drop down box doesn't get printed to the next page too

Link to comment
Share on other sites

What you should make sure of is that you actually get the content from the page.

I as late as yesterday wasn't able to do that, because the webpage I tried get it from didn't like what I sent of user-agent.

You can read about that here:

http://davidwalsh.name/set-user-agent-php-curl-spoof

 

So make sure you actually get the content, try to echo it straight away, just for test.

$str = file_get_contents($_POST["turl"]);
if($str != false){
echo $str;
}else{
echo 'Wasn\'t able to get contents from file/url';
}

 

Also this line, are you sure you want the second h2 tag and not the first?

echo $items->item(1)->nodeValue . "<br/>";

 

I'm somewhat sure you want to do this instead:

echo $items->item(0)->nodeValue . "<br/>";

Link to comment
Share on other sites

I tried what you suggested and it does print the full page when echo'ed so I'm not sure what the problem is, also I do mean to grab the second <h2> because the second occurance of h2 on the page is the title (the first is a heading i don't need)

Link to comment
Share on other sites

I tried what you suggested and it does print the full page when echo'ed so I'm not sure what the problem is, also I do mean to grab the second <h2> because the second occurance of h2 on the page is the title (the first is a heading i don't need)

 

Good, then you should test if the DOMDocument's method loadHTML was able to parse the HTML, even though it should be able to parse it even if it's not properly formed. I'm not sure if it should have popped up any warnings or errors by now if there was anything wrong with it, but it doesn't hurt testing...

if($DOM->loadHTML($str) == false){
echo 'Wasn\'nt able to parse HTML';
}else{
echo 'Was able to parse HTML';
}

 

And then finally test if it actually was able to parse a second h2 tag:

echo $items->length;
if($items->item(1)->nodeValue == false){
echo 'Wasn\'t able to find a second h2 tag';
}else if($items->item(1)->nodeValue == ''){
echo 'The content of the second h2 tag was parsed as empty';
}else{
echo $items->item(1)->nodeValue . ', IT WORKED!';
}

Link to comment
Share on other sites

well I messed around a bit and its actually working! the only problem I have now is I can't figure out how to grab the selection that the user chose from the dropdown box on the previous page and display it as text under the title that is printed (hope that makes sense)

Link to comment
Share on other sites

well I messed around a bit and its actually working! the only problem I have now is I can't figure out how to grab the selection that the user chose from the dropdown box on the previous page and display it as text under the title that is printed (hope that makes sense)

Isn't that just to use this in the "parse.php" file:

$selection = $_POST['selectskill'];

Link to comment
Share on other sites

well I messed around a bit and its actually working! the only problem I have now is I can't figure out how to grab the selection that the user chose from the dropdown box on the previous page and display it as text under the title that is printed (hope that makes sense)

Isn't that just to use this in the "parse.php" file:

$selection = $_POST['selectskill'];

 

thanks very much! :)

 

that's everything fixed!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.