Margo Eastham Margo Eastham - 1 year ago 40
HTML Question

Html submit value based on which button is clicked

Is it possible to submit hidden name value pair based on which button is clicked?

Here's my form:

<form action="POST" action="">
<button>
<input type="hidden" name="vote" value="up"/>
vote up!
</button>
<button>
<input type="hidden" name="vote" value="down"/>
vote down
</button>
</form>


For some reason, the name value pair I received was always the latter (the first one got replaced). For example a user clicked on 'vote down', I still get
$input['vote']
=
up


Does anyone know how to fix this?

Answer Source

This is because you put both of them in the form so the parameters received by server will look like

vote=up&vote=down

Assume that you access it using php associative array you will always received the latest value in the entries having the same key. That aside, why not just

<form action="POST" action="">
<button>
    <input type="hidden" name="vote" value="up"/>
    vote up!
</button>
</form> 

<form action="POST" action="">
<button>
    <input type="hidden" name="vote" value="down"/>
    vote down
</button>
</form>