Skip to content

Instantly share code, notes, and snippets.

@mtasuandi
Created June 2, 2013 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtasuandi/5692416 to your computer and use it in GitHub Desktop.
Save mtasuandi/5692416 to your computer and use it in GitHub Desktop.
Fetching form fields without using RegEx
<?php
$genericform = HTML_FORM;
$document = new DOMDocument();
$document->loadHTML(stripslashes($genericform));
for($i=0; $i<$document->getElementsByTagName('input')->length; $i++)
{
$htmlform = $document->getElementsByTagName('input')->item($i);
if($htmlform->attributes->getNamedItem('type')->value == 'hidden')
{
$hiddenfields[][$htmlform->attributes->getNamedItem('name')->value] = $htmlform->attributes->getNamedItem('value')->value;
$hiddenfield[] = $htmlform->attributes->getNamedItem('name')->value;
$hiddenvalue[] = $htmlform->attributes->getNamedItem('value')->value;
}
if($htmlform->attributes->getNamedItem('type')->value == 'text' || $htmlform->attributes->getNamedItem('type')->value == 'email')
{
$inputfield[] = $htmlform->attributes->getNamedItem('name')->value;
}
}
$form = $document->getElementsByTagName('form')->item(0);
$action = $form->attributes->getNamedItem('action')->value;
$method = $form->attributes->getNamedItem('method')->value;
foreach($hiddenfield as $hfield)
{
$hidfield .= $hfield.':';
}
foreach($hiddenvalue as $hvalue)
{
if($hvalue == ''){$hidval='-';}else{$hidval=$hvalue;}
$hidvalue .= $hidval.':';
}
$email = $inputfield[0];
$name = $inputfield[1];
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment