is it possible to read an attribute from a form element?
I got this:
<input class="number required" min="1250" max="999999999" id="insert_counter1" name="my_counter" value="" type="text">
After submit I can access the value. But is there any way to get to the other attributes like "min" or "max"?
Right now I have a hidden readonly input field named my_counter_min where the "min" attribute is stored. I do not know if that is a work-a-round or if it actually is the only way to do it.
Br. Anders
UPDATED based on answers below:
Than开发者_开发技巧ks for good feedback. As I read the answers there are three good solution with some pros and cons.
- Store value in input type=hidden. Easy to do, but can be tampered with
- Use parser. Takes more time to code but original value will be fetched through a new web request. So no tampering
- Store value in DB. If the form is related to a specific rcord in the DB then storing extra values can make good sence and will not cost a lot of coding effort if other values have to go into and out of the DB allready (just add a value).
I got three different answers for my question. Fastest answer marked correct even though there are more than this one solution.
BR. Anders
No. Only the name and value will reach the server, so you will need a workaround (like the one which you described).
I do not know if that is a work-a-round or if it actually is the only way to do it.
It's the only good way to do it. The form elements' other attributes are not transmitted in the request.
They are not transmitted, but you could load the page with an HTML parser and extract the values.
The big question is, why do you want to do that? If you want to use min/max to make sure the submitted value is in this range, you should hardcode these values at the serverside instead of relying to get them from the clientside somehow.
Passing that range as hidden field is pointless because it can easily be tampered with. Pulling them with a parser makes sure they are not tampered but adds the overhead of a second request.
Interesting. I had the same problem once, and fixed it the same way you did. Never actually looked back at the problem.
One thing I can think of is storing the values in a database, so you can: 1. Use them as input variable. 2. Access it any time you want to.
Ps: Originally this was a comment, but seeing as you'r accepting alternatives.
精彩评论