Php : Get name and value of all input tags on a page with DomDocument

The following code snippet will extract all input tag names and values as an associative array, from a given html page.

	Generic function to fetch all input tags (name and value) on a page
Useful when writing automatic login bots/scrapers
function get_input_tags($html)
	$post_data = array();
	// a new dom object
    $dom = new DomDocument; 
	//load the html into the object
    //discard white space
    $dom->preserveWhiteSpace = false; 
    //all input tags as a list
    $input_tags = $dom->getElementsByTagName('input'); 

    //get all rows from the table
	for ($i = 0; $i < $input_tags->length; $i++) 
  		if( is_object($input_tags->item($i)) )
			$name = $value = '';
			$name_o = $input_tags->item($i)->attributes->getNamedItem('name');
				$name = $name_o->value;
				$value_o = $input_tags->item($i)->attributes->getNamedItem('value');
					$value = $input_tags->item($i)->attributes->getNamedItem('value')->value;
				$post_data[$name] = $value;
	return $post_data;


$html = file_get_contents("");

echo "<pre>";
echo "</pre>";

Last Updated On : 4th August 2012

Subscribe to get updates delivered to your inbox

About Silver Moon

Php developer, blogger and Linux enthusiast. He can be reached at [email protected]. Or find him on

4 Comments + Add Comment

  • Thanks a lot Bro. You saved my day. God Bless.

  • Thanks lot it’s help me lot..

  • Good! Thank you…

  • Thank You very much! GOD bless you.

Leave a comment