js send request code example

Example 1: GET req with js

function httpGet(theUrl) {
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
    xmlHttp.send( null );
    return xmlHttp.responseText;
}

Example 2: send info with get request in js

document.getElementById("form").onsubmit=function(e){
                e.preventDefault();
                var str=document.getElementById("val1").value;
                var xhr=new XMLHttpRequest();
                xhr.open("GET","new.php?val1="+str,true);
                xhr.onload=function(){
                    console.log(this.responseText);
                }
                xhr.send();
         }
//php file
<?php
if(isset($_GET['val1'])){
   
   $a=$_GET['val1'];
   echo $a;
}