Jump to content

Confusion of value attribute usage

- - - - -

  • Please log in to reply
2 replies to this topic

#1
farhanyun91

farhanyun91

    Learning Programmer

  • Members
  • PipPipPip
  • 53 posts
I need your help to enlighten me more about value attribute in form(html).what is the function of it actually as its has a difference functionality base on the input type.i have a 2 codes here which confuses me about the usage of value and the way to use it

here is the first one:

<form>
Time <input type="checkbox" name="time" value="subscribe">
<br>
<br>
Newsweek<input type="checkbox" name="newsweek" value="subscribe">
</form>

here is the second one:

<form>
<input type="checkbox" name="vehicle" value="Bike" /> I have a bike<br />
<input type="checkbox" name="vehicle" value="Car" /> I have a car
</form>

As i can see from the first code, name attribute is to name the input which is according to the name of the checkbox button and it is changeable base on checkbox button name,and i have no idea about the value attribute.

on the other hand, for the second code, i found that the name attribute has two same names which are vehicle while the value has two different name which are bike and car.

why does the first code has two different names but with same value while the second code has two same names with different values?Please explain to me in more depth about this case.Thank You.
Thanks for your kindness and I wish it will be rewarded in exchange with wisdom and good health.

#2
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
The first one are individual checkboxes, where the second are grouped.

#3
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
its a little bit more to it. it is about what the receiving script shall expect. in first exmple, if checked, you will get $_POST['time'] to have the value "subscribe" and if not checked, it won't be set at all.

the second example isn't good, there are two changes I would suggest, either use radio buttons, and the value will show which was choosen, or, add [] after the name, and all checked will make an array like:
$_POST['vehicle'][0] is "Car" and $_POST['vehicle'][1] is "Bike", or similar. this means that you ould loop through the array to see what the user selected. a multi-pick-select will work like this as well.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users