I've got post ajax request in vanilla javascript, all works well, but I need to add an variable to form_data
, which will be send to php. For example variable apple = 1
is already in form_data. I need to add orange = 2
, but I can't do it in my html form. My js code is below:
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);
}
To sum up: I want to add orange=1
to form_data. Anyone knows how to do it? My primary intention was to add information which button was clicked to differentiate ajax functions in php. My HTML code is below:
<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"/>
Add it using append()
function like:
//form_data.append(name, value);
form_data.append('orange',2);
Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.