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

246
Views
why php results in HTML are undefined when using JavaScript

I need to get the IP of the client. I am able to get it through PHP variable "$_SERVER['REMOTE_ADDR']". I get this ip from server side php to html page through AJAX request but when I want to use this IP value in JavaScript it is showing that the value is undefined. any solution?

PHP code:

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

HTML CODE:

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

JavaScript code:

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);
}
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

First of all you should validate that you are getting the right response from your AJAX request by check that the result is certainly written to the element with id attribute "ip", and than instead of using:

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

You should use Node.textContent to get the text content:

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

Code example (without AJAX request):

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>

about 4 years ago · Santiago Trujillo Report

0

You want your Ip Address in java script , so have to put ip address in that tag i think.

<?php $ip_address =  $_SERVER['REMOTE_ADDR'];?>

<body onload='ip(); ip2();'>
<kbd id='ip' ><?php echo $ip_address; ?></kbd>
about 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>

run this code you might found what is the problem.

about 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!