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

PREVIOUS NEXT
Code Example
Javascript :: template literal 
Javascript :: javascript set size 
Javascript :: JavaScript (SMonkey 60.2.3) sample 
Javascript :: color switcher 
Javascript :: url enocde in javascript 
Javascript :: suitescript dialog box 
Javascript :: insertmany 
Javascript :: open in new tab js html does not work on iphone 
Javascript :: optional css tippy 
Javascript :: js spin wheel color 
Javascript :: merge sort 
Javascript :: JavaScript, numbers are primitive data types 
Javascript :: javascript copy array using spread operator 
Javascript :: fetch second parameters 
Javascript :: django debug toolbar javascript error 
Javascript :: actionscript round roundnumber 
Javascript :: what does this operation tell if(!arr.some(isNaN)) JavaScript 
Javascript :: at runtime.exports.handler aws lambda 
Javascript :: use anchor element to open file 
Javascript :: Cntrlsss:$.Control-Ai 
Javascript :: phaser create animation on sprite 
Javascript :: .every() Accepts a test function and returns a boolean if all the elements of the array pass the test. 
Javascript :: JS table with rows that have alternating colours 
Javascript :: filter text js 
Javascript :: change on id 
Javascript :: .pop javascript 
Javascript :: how to make a string in javascript 
Javascript :: react native toggle button with text 
Javascript :: Multiple functions in javascript onclick 
Javascript :: take off element form end of array 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =