简体   繁体   中英

Is there a way to pass the **type** HTML input attribute value to the $_POST array in a HTML/PHP Form?

Can we somehow pass the type HTML input attribute value to the $_POST array or grab it anyhow else with PHP?

I am aware that I can create a hidden field and basically put the type of the real input into the value of the hidden field, but this seems a bit like "repeating" work to me.

I want to create a Form, where input values are submitted to the $_POST and I can detect the type of that input without the need to hardcode/map the single inputs to each a type.

In this way I could detect the field type and act upon without the need to create a "map" that maps my custom inputs (by name or ID) to a certain type, which I already declare in HTML form anyway.

It seems a real shortcoming that the type of an input is undetectable in a Form Submit - or perhaps (hopefully) I miss something?

Can we somehow pass the type HTML input attribute value to the $_POST array or grab it anyhow else with PHP?

Not per se.

I am aware that I can create a hidden field and basically put the type of the real input into the value of the hidden field

That is a way to do it.

It seems a real shortcoming that the type of an input is undetectable in a Form Submit

Usually you know what type of data you expect for a given field because you aren't processing them generically, so it would rarely be a useful feature.

perhaps (hopefully) I miss something?

No.

Well here is the breakdown;

GET accessed via $_GET in PHP tackling and POST accessed via $_POST in PHP are transport methods, so is PUT, and DELETE etc for a from it does not matter what method you use it only works on client side and only knows to map every thing in it into serialised query string or at least have it read for being serialised.

For example

<input type="text" id="firstname" name="fname">

it takes the name attribute and converts into this

?fname=ferret

See it didn't even bother with ID attribute. When we hit submit button form will only run through name attributes of each input and make LHS of the with value and add user input as RHS to the value. It will not do anything else at all.

On PHP side we ask $_GET tunnel are there any query strings in the request or $_POST tunnel. Each of these if there is any query string - emphasis on word string. explodes the string into array and gives it you. hence $POST['fname']. Looks something like this

$_POST = [
fname => 'ferret',
someothingelse => 'someothervalue']

SO what you are trying to do is or at least asking to do is...make browser change its BOM behaviour - which we cannot in real sense of the matter; to make form add some thing like this.

?fname=ferret,text 

?fname=ferret-text

?fname=ferret/text 

form by default will not do this, unless you run custom function updating each query before submit and that is pron to what we call escaping, 3/100 time you would miss it given the chance

Then on PHP side you want PHP to figure out on its own that after slash is type like so

$_POST = [
fname => 'ferret/text']

PHP would not do that on its own, unless you fork it make custom whatever like Facebook has done and then run it or at least make some kind of low level library but that too would be after the fact.

in case your not wondering, thats how XSS and injections happen.

SO query string standards are rigid to keep things a string with militaristic data and serialised.

So yes what you intended to do with hidden field is one tested way of achieving what you are want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM