我正在使用JQuery Ajax(我确信自上次使用它以来情况发生了变化)但是我无法从PHP变量中提取信息。基本上我正在获取IP地址并记录IP完全加载页面然后显示它需要多长时间。
这是我的代码......
getIP.php
<?php
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip = $_SERVER['REMOTE_ADDR'];
}
echo json_encode(array('ip' => $ip));
?>
调用它的事件监听器
var IPAddresses = [];
//Anonymous functions - used for quality control and logging
(function() { //Used to test how long it took for a user to connect - uses a php script with it
window.addEventListener("load", function() {
$.ajax({
url: '../php/getIP.php',
type: 'POST',
success: function(result)
{
setTimeout(function alertUser(){IPAddresses.push(result.ip);}, 40);
}
});
}, false);
})();
(function() {
window.addEventListener("load", function() {
setTimeout(function() {
for (var i = 0; i < IPAddresses.length; i++)
{
var timing = performance.timing;
console.log(IPAddresses[i] + " " + timing.loadEventEnd - timing.responseEnd);
}
}, 0);
}, false);
})();
修改
现在我没有收到错误,但它似乎没有打印IP地址或将其推入数组中。我基本上试图让它[ip] [loadtime]它给出NaN错误
答案 0 :(得分:3)
您的输出是一个字符串:
echo $ip; //Just to check if it worked - it shows the IP
^---e.g. 127.0.0.1
然后您尝试将其视为数组:
setTimeout(function alertUser(){alert(result['ip']);}, 40);
^^^^^^
由于它不是一个阵列,这不会起作用。试试alert(result)
。
答案 1 :(得分:0)
尝试使用“json_encode”
IntVector2
和ajax
<html>
<body>
<center>
<form method="POST">
Enter New Password:<input type="password" name="password"/><br \>
Confirm Password:<input type="password" name="conpassword"/><br \>
<button name="submit" value="Change My Password"/>
<button type="clear"/>
</form>
</center>
</body>
</html>
<?php
@mysql_connect("localhost","root","");
mysql_select_db("sarath");
if(isset($_POST['submit']))
{
session_start();
if(!isset($_SESSION['name']))
header("location:login.php");
$n=$_SESSION['name'];
$p=$_POST['password'];
$cp=$_POST['conpassword'];
if($p==$cp)
{
$query="UPDATE student
SET password='$p'
WHERE username='$n';";
$r=mysql_query($query);
if($r)
echo "password changed successfully";
}
else
echo "please enter the same password";
}
else
echo "something wrong";
?>