我有三个php页面和两个javascript / jquery函数。
的search.php:
在此页面中,jquery获取数据通过单击嵌入在锚标记中的img标记将数据发送到 LikeMail.php 。 while循环中的两个和其他html内容都是动态生成的。在将jquery get数据发送到 LikeMail.php 之后,它通过ajax调用调用该php页面。 jquery和ajax在这个页面都运行正常。 Jquery正确发送数据并通过ajax正确接收数据。
<?php
session_start();
include("db.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Blog</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="main.css">
<script src="jquery-1.12.2.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="user2.js"></script>
<style>
p.col-lg-2{
font-family: "Comic Sans MS";
}
</style>
</head>
<body>
<div class="container">
<nav class="navbar nav-top navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li class="link one"><a href="index.php">Home</a></li>
<li class="link">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Blog</a>
</li>
<li class="link"><a href="#">FAQ</a></li>
<li class="link"><a href="#">Terms & Cond</a></li>
<li class="link"><a href="#">Contact</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="one"><a href="logout.php">Logout</a></li>
</ul>
</div>
</div>
</nav>
<!--Navbar-->
<div class="row topsecond">
<div class="col-lg-12">
<h3 class="heading">Ads</h3>
</div>
</div>
<!--Ads-->
<div class="row">
<div class="col-lg-8">
<div class="col-lg-3 col-sm-5">
<a type="button" href="userOwnProfile.php" class="btn btn-lg btn-primary">My Profile</a>
</div>
<div class="col-lg-3 col-sm-5">
<button type="button" class="btn btn-lg btn-primary">Women List</button>
</div>
<div class="col-lg-3 col-sm-5">
<button type="button" class="btn btn-lg btn-primary">Message List</button>
</div>
</div>
<div class="col-lg-4">
<div class="col-lg-6 col-sm-5">
<a href="#">
<button type="button" class="btn btn-lg btn-primary">New Likes</button>
</a></div>
</a>
<div class="col-lg-6 col-sm-5">
<button type="button" class="btn btn-lg btn-primary">New Message</button>
</div>
</div>
</div>
<div class="col-lg-12 col-sm-12">
<div class="row">
<h3 class="heading"><u>MEN'S LIST - Filter Search Result by:</u></h3>
</div>
<div class="row">
<!-- <label class="control-label col-xs-12 col-lg-1" for="pwd"> State Living: </label>-->
<!--<div class="col-xs-8 col-xs-offset-1 col-lg-2">
<select class="form-control col-lg-1">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>-->
<div class="control-label col-xs-12 col-lg-2">
<label for="age from">
Country
</label>
<select class="form-control col-lg-2">
<option selected>All</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
<!--<label class="control-label col-xs-12 col-lg-1" for="pwd">State Living:</label>
<div class="col-xs-8 col-xs-offset-2 col-lg-2">
<select class="form-control col-lg-3">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>-->
<div class="control-label col-xs-12 col-lg-2">
<label for="age from">
State Origin
</label>
<select class="form-control col-lg-2">
<option selected>Choose</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
<!--<label class="control-label col-xs-12 col-lg-1" for="pwd">State Living:</label>
<div class="col-xs-8 col-xs-offset-2 col-lg-2">
<select class="form-control ">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>-->
<div class="control-label col-xs-12 col-lg-2">
<label for="age from">
State Living
</label>
<select class="form-control col-lg-2">
<option selected>Choose</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
<!--<label class="control-label col-xs-12 col-lg-1" for="pwd">State Living:</label>
<div class="col-xs-8 col-xs-offset-2 col-lg-2">
<select class="form-control ">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>-->
<div class="control-label col-xs-12 col-lg-2">
<label for="age from">
Marital Status
</label>
<select class="form-control col-lg-2">
<option selected>Choose</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
<!--<label class="control-label col-xs-12 col-lg-1" for="pwd">State Living:</label>
<div class="col-xs-8 col-xs-offset-2 col-lg-2">
<select class="form-control col-lg-3">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
</div>
-->
<div class="control-label col-xs-12 col-lg-2">
<label for="age from">
Marital Status
</label>
<select class="form-control col-lg-2">
<option selected>Choose</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
<div class="col-xs-6 col-lg-1">
<label for="age from">
Age from
</label>
<input class="form-control" type="text">
</div>
<div class="col-xs-6 col-lg-1">
<label for="age to">
Age to
</label>
<input class="form-control" type="text">
</div>
</div>
</div>
<div class="col-lg-4">
</div>
<div class="row">
<div class="col-lg-2">
</div>
<div class="col-lg-12">
<?php
$query = mysqli_query($conn, "SELECT * FROM user");
while ($record = mysqli_fetch_array($query)) {
if ($record == FALSE) {
die(mysqli_error("error message for the user"));
}
?>
<div style="border: 1px dotted black;" class="row" id="call">
<div style=" border: 1px solid white" class="col-lg-2">
<!--<img src="images/team-hire.jpg" style="background-size: cover" width="85%" height="90%">-->
<a class="profile" href="<?php echo "viewProfile.php?id=" . $record['user_id'];?>" >
<img class="img-rounded"
src=" <?php echo "../shadi/images/" . $record['user_photo1'] ?>"
id="<?php echo $record['user_id']; ?>"alt="" width="70%" height="20%">
</a>
</div>
<p class="col-lg-2 col-xs-12 col-xs-offset-6">
<?php echo $record['user_educationLevel']; ?>
</p>
<p class="col-lg-2 col-xs-12">
<?php echo $record['user_career'] ?>
</p>
<p class="col-lg-2 col-xs-12">
<?php echo $record['user_stateLiving'] ?>
</p>
<p class="col-lg-2 col-xs-12">
<?php echo $record['user_stateOfOrigin'] ?>
</p>
<p class="col-lg-2 col-xs-12">
<?php echo $record['user_religion'] ?>
</p>
</div>
<?php
}
?>
</div>
</div>
</div>
</body>
</html>
viewProfile.php:
这是存在问题的页面。它基本上是重定向的页面。其url存储在 Search.php 页面中的锚标记中。在这个页面中,ajax从 LikeMail.php 返回错误的数据,因为我们之前通过 Search.php 发送的jquery获取数据未在此页面中返回。
<html>
<head>
</head>
</html>
<?php
session_start();
include("db.php");
if(!isset($_SESSION['login'])) {
header("Location:Login.php");
}
else {
$user1 = $_SESSION['user_id'];
if ($user_id == "") {
header("location:index.php");
} else {
//echo $user_id;
//$user_id = 14;
$user_email = $_SESSION['user_email'];
/*$sql = "UPDATE user SET user_onlineStatus= . NOW() . WHERE user_id=14";
$setLogged = mysqli_query($conn, $sql);*/
//echo $_SESSION['user_id'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User's Own Profile</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="main.css">
<script src="js/jquery-1.12.2.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="bootstrap-3.3.6-dist/js/jquery-1.12.2.min.js"></script>
<script src="bootstrap-3.3.6-dist/js/bootstrap.min.js"></script>
<!--<script src="js/custom.js"></script>-->
<script src="test.js"></script>
</head>
<body>
<div class="container">
<nav class="navbar nav-top navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li class="link one"><a href="index.php">Home</a></li>
<li class="link">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Blog</a>
</li>
<li class="link"><a href="#">FAQ</a></li>
<li class="link"><a href="#">Terms & Cond</a></li>
<li class="link"><a href="#">Contact</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="one"><a href="logout.php">Logout</a></li>
</ul>
</div>
</div>
</nav>
<!--Navbar-->
<div class="row topsecond">
<div class="col-lg-12">
<h3 class="heading">Ads</h3>
</div>
</div>
<!--Ads-->
<div class="row">
<div class="col-lg-8">
<div class="col-lg-4 col-sm-5">
<a type="button" href="userOwnProfile.php" class="btn btn-lg btn-primary">My Profile</a>
</div>
<div class="col-lg-4 col-sm-5">
<a type="button" href="Search.php" class="btn btn-lg btn-primary">Find People</a>
</div>
<div class="col-lg-4 col-sm-5">
<button type="button" class="btn btn-lg btn-primary">Message List</button>
</div>
</div>
<div class="col-lg-4">
<div class="col-lg-6 col-sm-5">
<a href="#">
<button type="button" class="btn btn-lg btn-primary">New Likes</button>
</a></div>
</a>
<div class="col-lg-6 col-sm-5">
<button href="index.php" type="button" class="btn btn-lg btn-primary">New Message</button>
</div>
</div>
</div>
<!--Links-->
<div class="col-lg-8 left">
<div class="row">
<div class="row ">
<?php
// include("db.php");
//$user_id = 14;
$photo = mysqli_query($conn, "SELECT * FROM user WHERE user_id='$user_id'");
while ($recordPhoto = mysqli_fetch_array($photo)) {
if ($recordPhoto == FALSE) {
die(mysqli_error("error message for the user"));
}
?>
<div class="col-lg-4 col-sm-4 col-xs-4 img">
<img class="img-rounded" src="<?php echo "../shadi/images/" . $recordPhoto['user_photo1'] ?>" alt=""
width="100%" height="40%">
</div>
<?php if($recordPhoto['user_photo2']!=""){
?>
<div class="col-lg-4 col-sm-4 col-xs-4 img">
<img class="img-rounded" src="<?php echo "../shadi/images/" . $recordPhoto['user_photo2'] ?>" alt="" width="100%" height="40%">
</div><?php }
else
{
echo "";
}
if($recordPhoto['user_photo2']!=""){
?>
<div class="col-lg-4 col-sm-4 col-xs-4 img">
<img class="img-rounded" src="<?php echo "../shadi/images/" . $recordPhoto['user_photo3'] ?>" alt="" width="100%" height="40%">
</div>
<?php }
else
{
echo "";
}
?>
</div>
<div class="row upload">
<div class="col-lg-6 col-sm-5 col-xs-12">
<div id="Like"></div>//This is the div where ajax data is returning through LikeMail.php
</div>
<a href="indexchat.php?chat=<?php echo $user_id; ?>">
<div class="col-lg-6 col-sm-5 col-xs-12">
<div class="btn btn-lg btn-info edit">
<span class="glyphicon glyphicon-comment"></span> Messages
</div>
</div>
</a>
</div>
</div>
<?php
$res = mysqli_query($conn, "SELECT * FROM user WHERE user_id=$user_id");
while ($record = mysqli_fetch_array($res)) {
if ($record == FALSE) {
die(mysqli_error("error message for the user"));
}
?>
<div class="col-lg-12 col-sm-12">
<div class="row">
<h3 class="heading col-sm-4">PROFILE</h3>
<div class="col-sm-8">
<h3><?php echo $record['user_name']; ?></h3>
</div>
</div>
<div class="">
<div class="row">
<h4 class="col-lg-4 col-sm-4 col-xs-4">Gender :</h4>
<div class="col-lg-6 col-sm-6 col-lg-6 col-xs-4 col-xs-8">
<h5><?php echo $record['user_gender']; ?></h5>
</div>
</div>
</div>
<div class="">
<div class="row">
<h4 class=" col-sm-4 col-lg-4 col-xs-4">Date of Birth :</h4>
<div class="col-sm-6 col-lg-6 col-xs-6">
<h5><?php echo $record['user_dob']; ?></h5>
</div>
</div>
</div>
<div class="">
<div class="row">
<h4 class=" col-sm-4 col-lg-4 col-xs-4">Age :</h4>
<div class="col-sm-6 col-lg-6 col-xs-4 col-sm-6 col-lg-6 col-xs-4 col-xs-12">
<h5>31 years</h5>
</div>
</div>
</div>
<div class="">
<div class="row">
<h4 class=" col-sm-4 col-lg-4 col-xs-4">Height :</h4>
<div class="col-sm-6 col-lg-6 col-xs-6">
<h5><?php echo $record['user_height']; ?></h5>
</div>
</div>
</div>
<div class="">
<div class="row">
<h4 class=" col-sm-4 col-lg-4 col-xs-4">Weight :</h4>
<div class="col-sm-6 col-lg-6 col-xs-6">
<h5><?php echo $record['user_weight']; ?></h5>
</div>
</div>
</div>
<div class="">
<div class="row">
<h4 class=" col-sm-4 col-lg-4 col-xs-4">Race :</h4>
<div class="col-sm-6 col-lg-6 col-xs-6">
<h5><?php echo $record['user_race']; ?></h5>
</div>
</div>
</div>
<div class="">
<div class="row">
<h4 class=" col-sm-4 col-lg-4 col-xs-4">Religion :</h4>
<div class="col-sm-6 col-lg-6 col-xs-6">
<h5><?php echo $record['user_religion']; ?></h5>
</div>
</div>
</div>
<br>
<div class="">
<div class="row">
<h4 class=" col-sm-4 col-lg-4 col-xs-4">Country :</h4>
<div class="col-sm-6 col-lg-6 col-xs-6">
<h5> <?php echo $record['user_country']; ?></h5>
</div>
</div>
</div>
<div class="">
<div class="row">
<h4 class=" col-sm-4 col-lg-4 col-xs-4">State Origin :</h4>
<div class="col-sm-6 col-lg-6 col-xs-6">
<h5> <?php echo $record['user_stateOfOrigin']; ?>
</h5></div>
</div>
</div>
<div class="">
<div class="row">
<h4 class=" col-sm-4 col-lg-4 col-xs-4">State Living :</h4>
<div class="col-sm-6 col-lg-6 col-xs-6">
<h5> <?php echo $record['user_stateLiving']; ?></h5>
</div>
</div>
</div>
<div class="">
<div class="row">
<h4 class=" col-sm-4 col-lg-4 col-xs-4">Marital Stauts :</h4>
<div class="col-sm-6 col-lg-6 col-xs-6">
<h5><?php echo $record['user_maritalStatus']; ?></h5>
</div>
</div>
</div>
<div class="">
<div class="row">
<h4 class=" col-sm-4 col-lg-4 col-xs-4">Career :</h4>
<div class="col-sm-6 col-lg-6 col-xs-6">
<h5><?php echo $record['user_career']; ?></h5>
</div>
</div>
</div>
<div class="">
<div class="row">
<h4 class=" col-sm-4 col-lg-4 col-xs-4">Educational Level :</h4>
<div class="col-sm-6 col-lg-6 col-xs-6">
<h5><?php echo $record['user_educationLevel']; ?></h5>
</div>
</div>
</div>
<br>
<div class="">
<div class="row">
<h4 class="col-sm-4 col-lg-4 col-xs-4">Relations for</h4>
<div class="col-sm-6 col-lg-6 col-xs-6">
<h5><?php echo $record['user_relationshipfor']; ?></h5>
</div>
</div>
</div>
</div>
<div class="row">
<h4 class=" col-sm-4 col-lg-4 col-xs-4">Self Summary : <br>
</h4>
<div class="col-sm-6 col-lg-6 col-xs-6">
<h5><?php echo $record['user_selfSummary']; ?></h5>
</div>
</div>
<br>
<div class="row">
<div class="col-sm-4">
</div>
<!--<div class="col-sm-8">
<button type="button" class="btn btn-lg btn-warning">Submit</button>
<div class="respond"></div>
</body>
</html>
<?php
}
?>
user2.js:
这是get2在user2.js页面中的jquery函数
$(document).ready(function () {
$('.profile').on('click', function (e) {
//Get the href Link
var href = $(this).attr('href');
e.preventDefault();
$.get("LikeMail.php",
{user2: $(this).find(".img-rounded").attr("id")}, function (returnedData) {
// Do whatever you want with returend data
console.log(returnedData),
function(){
console.log('error');
}
}).done(function () {
//Redirecting to anchor destination.
window.location.href = href;
});
});
});
中
function like()
{
var req = new XMLHttpRequest();
req.onreadystatechange = function()
{
if(req.readyState==4 && req.status==200)
{
document.getElementById('Like').innerHTML=req.responseText;
}
}
req.open('GET','LikeMail.php','true');
req.send();
}
setInterval(function(){like()},1000);
我想知道为什么在Search.php中通过锚标记重定向后,为什么jquery获取数据没有在vieProfile.php页面中接收
更新:
Ajax将在<div id="Like"></div>
中返回viewProfile.php