How to send POST request using html button

2019-07-21 01:10发布

I want to send post request using html button. I know it is easily done by jQuery ajax, but i do not want to use jquery and ajax. How can i do it?

This my function

public function editProduct() {
    if($_SERVER['REQUEST_METHOD'] === 'POST') {
        echo 'You are authorized';
    } else {
        echo 'You are not authorized to access this page.';
    }
}

This is my HTML button

<button type="submit" onclick="location.href = '<?=base_url().'company/admin/add_product/editProduct?>';">Send Post Request</button>

2条回答
倾城 Initia
2楼-- · 2019-07-21 01:32

In your form tag, just write a method="post"

<form method="post">
 ...
    <button type="submit" >
</form>
查看更多
何必那么认真
3楼-- · 2019-07-21 01:47

You have two ways to do this:

  1. via form (best method):

    <form action ="<?php echo base_url().'company/admin/add_product/editProduct'?>" method="post">
          <input type="submit" value="Send Post Request">
    </form>
    
  2. via javascript (jquery: http://api.jquery.com/jquery.post/ )

查看更多
登录 后发表回答