Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

257
Views
por qué los resultados de php en HTML no están definidos cuando se usa JavaScript

Necesito obtener la IP del cliente. Puedo obtenerlo a través de la variable PHP "$_SERVER['REMOTE_ADDR']". Obtengo esta IP del php del lado del servidor a la página html a través de la solicitud AJAX, pero cuando quiero usar este valor de IP en JavaScript, muestra que el valor no está definido. ¿alguna solución?

código PHP:

 <?php echo $_SERVER['REMOTE_ADDR'];?>

CÓDIGO HTML:

 <body onload='ip(); ip2();'> <kbd id='ip' ></kbd>

Código JavaScript:

 function ip() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { document.getElementById("ip").innerHTML = this.responseText; } }; xhttp.open("POST", "ip.php"); xhttp.send(); } function ip2() { setTimeout(function () { var ip = document.getElementById("ip").value; alert(ip); }, 1000); }
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

En primer lugar, debe validar que está obteniendo la respuesta correcta de su solicitud AJAX al verificar que el resultado esté ciertamente escrito en el elemento con el atributo de id "ip", y que en lugar de usar:

 var ip = document.getElementById('ip').value;

Debe usar Node.textContent para obtener el contenido del texto:

 var ip = document.getElementById('ip').textContent;

Ejemplo de código (sin solicitud AJAX):

 function ip() { document.getElementById('ip').innerHTML = '127.0.0.1'; } function ip2() { setTimeout(function () { var ip = document.getElementById('ip').textContent; console.log(ip); }, 1000); }
 <body onload="ip(); ip2();"> <kbd id="ip" ></kbd>

over 4 years ago · Santiago Trujillo Report

0

Desea su dirección IP en java script, así que creo que tiene que poner la dirección IP en esa etiqueta.

 <?php $ip_address = $_SERVER['REMOTE_ADDR'];?> <body onload='ip(); ip2();'> <kbd id='ip' ><?php echo $ip_address; ?></kbd>
over 4 years ago · Santiago Trujillo Report

0

<?php echo $_SERVER['REMOTE_ADDR'];?> <html> <head> </head> <body onload='ip();'> <div id='ip' ></div> </body> </html> <script> function ip() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("ip").innerHTML = this.responseText; ip2(this.responseText); } }; xhttp.open("POST", "try.php"); xhttp.send(); } function ip2(stringvalue) { setTimeout( function() { var ip = document.getElementById("ip").value; alert(stringvalue); },2000); } </script>

ejecute este código, es posible que encuentre cuál es el problema.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!