Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML input type submit value without modifiying post value?

Lets say I have a really simple html form:

<form action="./whatever.php" method="POST">
    <input type="submit" name="TheButton" value="Apples">
</form>

Which of course makes the button the user sees say Apples. I want the post request to be TheButton=Oranges ... Is there a simple way to deal with this in HTML, or will I be need to do something with a action=javascript ?

like image 845
Incognito Avatar asked Oct 27 '10 13:10

Incognito


1 Answers

The simple way is:

<button type="submit" name="TheButton" value="Oranges">Apples</button>

… but this will break in Internet Explorer (IIRC, up to and including version 7).

If you only have one button to deal with then:

<input type="submit" value="Apples">
<input type="hidden" name="TheButton" value="Oranges">

… will work fine.

Otherwise the best approach is to use server side logic:

<input type="submit" name="TheButton_Oranges" value="Apples">

And look for a value which starts with TheButton_ and then extract the value from the name. It is an ugly hack, but it is reliable and doesn't depend on client side JS.

like image 187
Quentin Avatar answered Nov 15 '22 07:11

Quentin