How to send POST request using html button

六月ゝ 毕业季﹏ 提交于 2020-02-04 07:02:29

问题


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>

回答1:


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

<form method="post">
 ...
    <button type="submit" >
</form>



回答2:


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/ )



来源:https://stackoverflow.com/questions/30864319/how-to-send-post-request-using-html-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!