$("#id").click(function(){
//Read the fields
let vnm = $('#first_name').val();
let anm = $('#last_name').val();
//Check if the fields are filled
if (vnm == "")
{
$("#result").html("Fill in a first name!");
}
else if (anm == "")
{
$("#result").html("Enter a surname!");
}
//When the fields are filled, the data are to the processing page
else
{
$.ajax({
type: "POST",
url: "post.php",
data: {"First Name": vnm,
"last name": anm},
success: function (tekst) {
$("#result").html(tekst);
},
error: function (request, error) {
console.log ("ERROR:" + error);
}
});
}
//Note: form may not be 'sent'!
return false;
});
function myFunction()
{
var elements = document.getElementsByClassName("formVal");
var formData = new FormData();
for(var i=0; i<elements.length; i++)
{
formData.append(elements[i].name, elements[i].value);
}
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
alert(xmlHttp.responseText);
}
}
xmlHttp.open("post", "server.php");
xmlHttp.send(formData);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Ajax POST Demo</title>
<script>
function postComment() {
// Creating the XMLHttpRequest object
var request = new XMLHttpRequest();
// Instantiating the request object
request.open("POST", "/examples/php/confirmation.php");
// Defining event listener for readystatechange event
request.onreadystatechange = function() {
// Check if the request is compete and was successful
if(this.readyState === 4 && this.status === 200) {
// Inserting the response from server into an HTML element
document.getElementById("result").innerHTML = this.responseText;
}
};
// Retrieving the form data
var myForm = document.getElementById("myForm");
var formData = new FormData(myForm);
// Sending the request to the server
request.send(formData);
}
</script>
</head>
<body>
<form id="myForm">
<label>Name:</label>
<div><input type="text" name="name"></div>
<br>
<label>Comment:</label>
<div><textarea name="comment"></textarea></div>
<p><button type="button" onclick="postComment()">Post Comment</button></p>
</form>
<div id="result">
<p>Content of the result DIV box will be replaced by the server response</p>
</div>
</body>
</html>
$.ajax({
type: "POST",
url: "https://reqbin.com/echo/post/json",
data: `{
"Id": 78912,
"Customer": "Jason Sweet",
}`,
success: function (result) {
console.log(result);
},
dataType: "json"
});