-2

I need to check the type of the different inputs I have in a form.

I need to know if the field xxx is a checkbox, radio, select, etc.

Is it possible?

Thanks!

  • César -
César
  • 1

3 Answers3

2

Assuming you mean after submitting the form, then no, only the parameter name and value are sent to the server. You could be a little clever with your naming to identify them server-side. At the server, you can then iterate over the request variables and parse chk from the name. For example:

HTML

<input type="checkbox" name="chkMyCheck" value="1" />

PHP

foreach ($_GET as $key => $value) {
    if (substr($key, 0, 3) == "chk")
        echo 'Checkbox '.$key.' submitted with value '.$value;
}

It makes it harder to get the variables at the server, though, if you don't know the type beforehand.

Andy E
  • 338,112
  • 86
  • 474
  • 445
  • A type of [Hungarian notation](http://en.wikipedia.org/wiki/Hungarian_notation) I guess? – jensgram Jan 18 '11 at 12:23
  • @jensgram: something like that :-) I remember Visual Basic 6 used to name objects like that by default. They changed it in .NET, though. – Andy E Jan 18 '11 at 12:24
0

Yes, you can use an HTML parser and fetch the input element's type attribute.

$html = <<< HTML
<form>
    <input type="submit" value="Submit"/>
    <input type="hidden" value="1234"/>
    <input type="text" value="some text" id="xxx"/>
</form>
HTML;

// fetch with
$dom = new DOMDocument;
$dom->loadHTML($html); // use loadHTMLFile to load from file or URL
echo $dom->getElementById('xxx')->getAttribute('type');

will output "text".

There is plenty of examples on SO that cover fetching different parts. Just give it a search.

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
0

The only data types that reach PHP are strings and arrays, so it's not possible to do that directly.

The way I see it you only have two options:

  1. Use Hungarian-like notation, or
  2. Parse the form DOM with SimpleXML or similar
Alix Axel
  • 151,645
  • 95
  • 393
  • 500