Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a HTML button perform a POST request?

I want a submit type button to send a POST request.

I am thinking about something like this:

<form action = "" method = "post">     <button>Upvote</button> <form> 

where the string "Upvote" will be send as a name in the POST request.

I know this is not working, and I know there are ways using AJAX(javascript) but I am fairly new to this area. I am just wondering if this is possible in general.

Update

Someone suggested using the <input> tag, and I tried it. The problem is it generates a GET rather than a POST.

like image 883
Bob Fang Avatar asked Apr 16 '13 11:04

Bob Fang


2 Answers

You need to give the button a name and a value.

No control can be submitted without a name, and the content of a button element is the label, not the value.

<form action="" method="post">     <button name="foo" value="upvote">Upvote</button> </form> 
like image 195
Quentin Avatar answered Oct 18 '22 12:10

Quentin


This can be done with an input element of a type "submit". This will appear as a button to the user and clicking the button will send the form.

<form action="" method="post">     <input type="submit" name="upvote" value="Upvote" /> </form> 
like image 41
Filip Minx Avatar answered Oct 18 '22 13:10

Filip Minx