2 years ago

#71129

test-img

NdeNoob

Sending parameters with xmlhttprequest

Im trying to send paramaters with xmlhttprequest but I dont know how. I have an html code with a form and where I create the xmlhttprequest objet and a php code where I want to send the parameters of my form and show them. So, the thing is that it show me the Notice: Undefined variable error, as the variables are not correctly sent. Both codes are in my xampp/htdocs folder.

These are the codes:

HTML & JS:

<!DOCTYPE html>
<html lang="en">
<head>
   
</head>
<body>

    
    <form name="ajax" method="post">
        
        
        
        Surname: <input id="surname" type="text" required/>
        <br>    
        <br>                  
        Name: <input id="name" type="text" required />
        <br>      
        <br>        
        Phone: <input id="phone" type="tel" required/>
        <br>         
        <br>     
        <input type="submit" value="Send" name="send">

    </form>

      <script>
            
            var surname=document.getElementById("surname");
            var name=document.getElementById("name");
            var phone=document.getElementById("phone");

            var xhttp = new XMLHttpRequest();

            

            xhttp.onreadystatechange = function (){

            if (this.readyState == 4 && this.status==200){
                document.write(this.response);
            }
            };
            xhttp.open("GET","http://localhost/htdocs/show.php surname="+surname+"&name="+name+"&phone="+phone,false);
            xhttp.send();
            
      </script>
</body>
</html>

PHP called "show.php":

<?php


$surname;
$name;
$phone;


echo $surname."<br>".$name."<br>".$phone;


?>

Thank you in advance

javascript

php

xml

parameters

xmlhttprequest

0 Answers

Your Answer

Accepted video resources