Estoy tratando de enviar un valor de texto de entrada "Nombre de usuario" a un script php que está dando un resultado. Ese resultado lo quiero de vuelta en la página html.
<input type="text" id="user" name="user" placeholder="Username"> <button type="button" id="fetchBtn">Click Me!</button> <p id="txt"></p>Javascript dentro de mi sitio web
<script> let fetchBtn = document.getElementById('fetchBtn'); fetchBtn.addEventListener('click', buttonClickHandler); function buttonClickHandler() { // Instantiate an xhr object var xhr = new XMLHttpRequest(); // What to do when response is ready xhr.onreadystatechange = () => { if(xhr.readyState === 4) { if(xhr.status === 200) { document.getElementById("txt").innerHTML = xhr.responseText; } else { console.log('Error Code: ' + xhr.status); console.log('Error Message: ' + xhr.statusText); } } } xhr.open('GET', 'data.php'); // Send the request xhr.send(); } </script>Ahora estoy tratando de devolver la variable PHP $título a la página HTML después de que el script php la procese. No puedo encontrar la manera de mostrarlo correctamente
<?php function file_get_contents_curl($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $data = curl_exec($ch); curl_close($ch); return $data; } $para = $_GET['user']; $html = file_get_contents_curl("http://website.com/$para"); //parsing begins here: $doc = new DOMDocument(); @$doc->loadHTML($html); $nodes = $doc->getElementsByTagName('title'); //get and display what you need: $title = $nodes->item(0)->nodeValue; ?>No está devolviendo ningún dato, el XHR es GET, en cuyo caso tendrá que consultar vars al final de la url data.php?user='+user.value , mejor usar POST
y en su usuario PHP $user = $_GET['user'];
Pero no 100% lo que estás preguntando, sino tus ejemplos de código ...