Estoy tratando de enviar parámetros con xmlhttprequest pero no sé cómo. Tengo un código html con un formulario y donde creo el objeto xmlhttprequest y un código php donde quiero enviar los parámetros de mi formulario y mostrarlos. Entonces, el caso es que me muestra el Aviso: Error de variable indefinida , ya que las variables no se envían correctamente. Ambos códigos están en mi carpeta xampp/htdocs.
Estos son los codigos:
HTML y 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 llamado "mostrar.php":
<?php $surname; $name; $phone; echo $surname."<br>".$name."<br>".$phone; ?>Gracias de antemano