我有问题,Jquery Post不发送数据到其他页面。通过单击image将数据发送到LikeMail.php页面。日期存储在图像的id中。
<a href="viewProfile.php?id=<?php echo $record['user_id']; ?>"><img class="img-rounded" id="<?php echo $record['user_id']; ?>" src=" <?php echo "../shadi/images/" . $record['user_photo1'] ?>" alt=""
width="70%" height="20%"> </a>
这是我的LikeMail.php页面
<?php
session_start();
?>
<html>
<head>
<script src="jquery-1.12.2.min.js"></script>
<script src="test.js"></script>
</head>
</html>
<?php
include("db.php");
if(!isset($_SESSION['login'])) {
echo "";
}
else {
$user1 = $_SESSION['user_id'];
// echo $user1;
if(isset($_POST['fname'])) {//This is being received from jquery
$user2 = $_POST['fname'];
//$lname = $_POST['surname'];
//echo $lname;
/*$sql = "UPDATE notification SET alert='$fname' WHERE id = '1'";
if(mysqli_query($conn,$sql))
echo "updated";*/
$check_for_likes = mysqli_query($conn, "SELECT * FROM liked WHERE user1='$user1' AND user2='$user2'");
$numrows_likes = mysqli_num_rows($check_for_likes);
if (false == $numrows_likes) {
echo mysqli_error($conn);
}
if ($numrows_likes >= 1) {
echo '<input type="submit" name="unlikebutton_' . '" value="Unlike" id="Unlike" class="btn btn-lg btn-info edit">';
}
if ($numrows_likes == 0) {
echo '<input type="submit" name="likebutton_' . '" value="Like" id="Like" class="btn btn-lg btn-info edit">';
}
}
}?>
从上一页接收到具有变量fname
的POST数组(也在代码的注释中提到)。
$(document).ready(function(){
$('.img-rounded').click(function(){
$.post("LikeMail.php",
{fname: this.id},
function(data){
$('.respond').html(data);
}
);
});
});
你能告诉我那是什么毛病吗?我在其他页面上运行过这个jquery方法除了 likemail。php它运行得很好但是jquery post没有向 likemail。php
尽量防止<a>
点击时发生的默认行为,并且可能以防止事件传播。
$('.img-rounded').click(function(e){
e.stopPropagation(); // not sure if needed
e.preventDefault();
$.post("LikeMail.php",
{fname: this.id},
function(data){
$('.respond').html(data);
}
);
});
如果我删除<a>
标签,它可以工作。
请参阅https://jsfiddle.net/8ywdb7uh/上的代码(查看控制台消息,它将以404响应)
请阻止默认行为