我在vanilla javascript中发布了ajax请求,一切正常,但我需要将一个变量添加到form_data
,这将发送到php。
例如,变量apple = 1
已经在form_data中。我需要添加orange = 2
,但我无法在我的HTML格式中执行此操作。我的js代码如下:
function button_publish_ajax(variable){
var form = document.getElementById("form-buttons");
var form_data = new FormData(form);
for ([key,value] of form_data.entries()){
console.log(key + ': '+value);
}
var action = form.getAttribute("action");
var xhr = new XMLHttpRequest();
xhr.open('POST', action, true);
xhr.overrideMimeType('text/xml; charset=UTF-8');
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.onreadystatechange = function (){
if (xhr.readyState == 4 && xhr.status == 200){
var result = xhr.responseText;
console.log("Result:" + result);
}
};
xhr.send(form_data);
}
总结一下:我想将orange=1
添加到form_data。谁知道怎么做?
我的主要目的是添加点击按钮的信息,以区分php中的ajax函数。我的HTML代码如下:
<form method="post" action="cms_offers_button.php" id="form-buttons">
<input type="hidden" name="offer_list" value="12">
<input name="PublishListItem" type="button" class="btn btn-info offer-publish" value="Publish" id="publish-12" style="display: none;"/>
<input name="WithdrawListItem" type="button" class="btn btn-info offer-withdraw" value="Withdraw" id="withdraw-12"/>
<input name="EditListItem" type="button" class="btn btn-default offer-edit" value="Edit" id="edit-12" />
<input name="DeleteListItem" type="button" class="btn btn-danger offer-delete" value="Delete" id="delete-12"/>
答案 0 :(得分:3)