PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
d.shankar
Forum Newbie
Posts: 2 Joined: Wed Aug 22, 2007 2:32 am
Post
by d.shankar » Wed Aug 22, 2007 2:34 am
The below code i posted fetches an html page and searches for the occurence of forms .. And if found the form values are printed
But i need to print also the input text variables associated with the form [they should be mutually dependent]
below is the code
Code: Select all
<?php
$target_url = "www.dnschart.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html= curl_exec($ch);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$params = $dom->getElementsByTagName('form');
foreach ($params as $param) {
echo $param -> getAttribute('name').'<br>';
if($param->hasChildNodes())
{
echo "true";
echo "<br>";
$children = $param->childNodes;
echo $children->getElementsByTagName('input').'<br>';
foreach($children as $child)
{
echo $child->getAttribute('name');
}
}
else
{
echo "false";
echo "<br>";
}
?>
Waiting for reply.
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Wed Aug 22, 2007 4:54 am
d.shankar wrote: But i need to print also the input text variables associated with the form [they should be mutually dependent]
Do you mean the value attribute of the input element?
Ollie Saunders
DevNet Master
Posts: 3179 Joined: Tue May 24, 2005 6:01 pm
Location: UK
Post
by Ollie Saunders » Wed Aug 22, 2007 5:33 am
I would use
SimpleXML and XPath to do this. Generally the DOM is really awkward for conditionally finding stuff.
I think one of these will be the xpath you are after:
Code: Select all
form//.[@name]
form//*[@name]
form//@name
This could be a function:
Code: Select all
function urlContents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'User agent here');
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
return curl_exec($ch);
}
d.shankar
Forum Newbie
Posts: 2 Joined: Wed Aug 22, 2007 2:32 am
Post
by d.shankar » Wed Aug 22, 2007 6:22 am
volka wrote: d.shankar wrote: But i need to print also the input text variables associated with the form [they should be mutually dependent]
Do you mean the value attribute of the input element?
No i need the variable names. I mean.
An example...
Code: Select all
<form action='1.php' method='post' name='frm1'>
<input type='text' name='txt1'>
</form>
<form action='2.php' method='post name='frm2'>
<input type='text' name='txt2'>
</form>
Now i have to retrieve
1.php
txt1
2.php
txt2