Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

ajax post

$("#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;
});
Comment

ajax post request javascript

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); 
}
Comment

Ajax Post request

<!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>
Comment

JavaScript POST request with jQuery Ajax

$.ajax({
  type: "POST",
  url: "https://reqbin.com/echo/post/json",
  data: `{
    "Id": 78912,
    "Customer": "Jason Sweet",
  }`,
  success: function (result) {
     console.log(result);
  },
  dataType: "json"
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: local storage check max size 
Javascript :: js reduce concat numbers 
Javascript :: rails routes default format json 
Javascript :: sweet alert 2 do action on confirm 
Javascript :: js before unload 
Javascript :: for key value in object javascript 
Javascript :: how to filter through array extracting only numbers in js 
Javascript :: $ is not defined 
Javascript :: javascript loop through all element children 
Javascript :: useRoutes exact path match in react 
Javascript :: jquery minified cdn 
Javascript :: increase-memory-limit not working node 
Javascript :: javascript auto scroll down slowly 
Javascript :: check if an id exists javascript 
Javascript :: getting the distance fo an element from the top jquery 
Javascript :: jqeury cdn 
Javascript :: generate random random number with fixed length 
Javascript :: import formik 
Javascript :: fetch data from api url 
Javascript :: Data path "" should NOT have additional properties(es5BrowserSupport 
Javascript :: add 1 year to current date javascript 
Javascript :: change hover css javascript 
Javascript :: How i can use “LIKE” operator in mongoose 
Javascript :: javascript set attribute href 
Javascript :: viewdata array mvc razor javascript 
Javascript :: react native metro api level 30 
Javascript :: adonisjs column default value 
Javascript :: check radio button is checked jquery 
Javascript :: daterangepicker change 
Javascript :: how to replace word from string in javascript 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =