126

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.

MonoWolfChrome
  • 218
  • 2
  • 15
Bob Fang
  • 6,963
  • 10
  • 39
  • 72

5 Answers5

96

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>
Homulvas
  • 524
  • 5
  • 18
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
78

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>
Filip Minx
  • 2,438
  • 1
  • 17
  • 32
25

You can do that with a little help of JS. In the example below, a POST request is being submitted on a button click using the fetch method:

const button = document.getElementById('post-btn');

button.addEventListener('click', async _ => {
  try {     
    const response = await fetch('yourUrl', {
      method: 'post',
      body: {
        // Your body
      }
    });
    console.log('Completed!', response);
  } catch(err) {
    console.error(`Error: ${err}`);
  }
});
<button id="post-btn">I'm a button</button>
Jasper Lichte
  • 500
  • 4
  • 11
5

Simply do:

<form action="" method="post" id="myForm">
    <button type="submit" class="btn btn-sm btn-info" name="something"><i class="fa fa-check"></i>Submit</button>
</form>

Or in the case that for aesthetic reasons you want the button outside the form, do:

<button type="submit" form="myForm" class="btn btn-sm btn-info" name="something"><i class="fa fa-check"></i>Submit</button>

I typically use this when my form is too long and I want a submit button at the beginning and also at the end of the page.

Chrishow
  • 363
  • 1
  • 5
  • 13
  • This sends a POST variable `something` with an empty string (`""`) as its contents. You can use this without setting the value attribute, to get which button was clicked e.g. in PHP, you would test `if(isset($_POST["someting"]))`. – Fanky Dec 31 '21 at 16:09
4

You can:

  • Either, use an <input type="submit" ..>, instead of that button.
  • or, Use a bit of javascript, to get a hold of form object (using name or id), and call submit(..) on it. Eg: form.submit(). Attach this code to the button click event. This will serialise the form parameters and execute a GET or POST request as specified in the form's method attribute.
UltraInstinct
  • 43,308
  • 12
  • 81
  • 104