Van icon

AJAX PHP Get

Links


    //PHP Files-> Ajax4.html, Process.php
    <button id="button">Get Name</button>

    <h2>GET FORM</h2>
    <form action="process.php" method="GET">
        <input type="text" name="name">
        <input type="submit" value="Submit">
    </form>

    <h2>AJAX GET FORM</h2>
    <form id="getForm">
        <input type="text" name="name" id="name1">
        <input type="submit" value="Submit">
    </form>


    //AJAX js
    document.getElementById('button').addEventListener('click', getName);
        document.getElementById('getForm').addEventListener('submit', getName2);

        function getName(){
            var xhr = new XMLHttpRequest();
            xhr.open('GET', 'process.php?name=Brad', true);

            xhr.onload = function(){
                console.log(this.responseText);
            }
        xhr.send();
        }
        function getName2(e){
            e.preventDefault();

            var name = document.getElementById('name1').value;
            var xhr = new XMLHttpRequest();
            xhr.open('GET', 'process.php?name='+name, true);

            xhr.onload = function(){
                console.log(this.responseText);
            }
        xhr.send();
        }