如何在PHP中将标题数据与表单发布一起发送

时间:2019-08-26 15:09:11

标签: php

在表单提交时,数据通过POST方法发送。与此同时,如何将授权和其他标题数据发送到该帖子URL?

<form action="https://www.example.com/foobar" method="post">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit" value="Submit">
</form>

此帖子URL目标仅在发送授权标头时才接受数据。

1 个答案:

答案 0 :(得分:1)

您应该使用这样的jquery帖子来做到这一点:

function submit(){
var settings = {
  "url": "https://www.example.com/foobar",
  "method": "POST",
  "headers": {
    "content-type": "application/json",
    "cache-control": "no-cache",
    "token": "your token" // here you can add your token for authorization 
  },
  "processData": false,
  "data": {"fname": $("#fname").val(), "lname": $("#lname").val()  } // your form data
}

$.ajax(settings).done(function (response) {
  console.log(response);
});
}

您的HTML应该看起来像这样:

  First name: <input type="text" id="fname" name="fname"><br>
  Last name: <input type="text" id="lname" name="lname"><br>
  <button onclick="submit()">Submit</button>