我一直在使用jquery,ajax和php处理Like and Different功能。我的问题有点难以理解。让我们先尝试理解它。
viewProfile.php
和LikeMail.php
。 LikeMail.php
中的ajax函数正在调用viewProfile.php
。
-----------------
| Like/Unlike |
-----------------
这是一个按钮,它实际上来自这个ajax函数的LikeMail.php
function like()
{
var req = new XMLHttpRequest();
req.onreadystatechange = function()
{
if(req.readyState==4 && req.status==200)
{
document.getElementById('Like').innerHTML=req.responseText;
}
}
req.open('POST','LikeMail.php','true');
req.send();
}
setInterval(function(){like()},1000);
HTML:
<div id="Like"></div>
此div中显示输出。 以上按钮可以是Like
或Unlike
取决于LikeMail.php
中的条件,这将在LikeMail.php
说明部分中进行说明。
当其中一个(按钮)Like
或Unlike
被点击时。然后它调用相应的jquery点击功能,向LikeMail.php
发送帖子请求。我在标题中提到Indirect
页面,因为Like
或Unlike
按钮实际存在于LikeMail.php
中页。但由于ajax调用,这些按钮显示在viewProfile.php
页面中。然后我通过viewProfile.php
将帖子请求发送到实际页面LikeMail.php
这是Unlike
按钮
$(document).ready(function(){
$('#Unlike').unbind().click(function(){
$.post("LikeMail.php",
{Unlike: this.id},
function(data){
$('#response').html(data);
}
);
});
});
是jquery帖子或Like
按钮
$(document).ready(function(){
$('#Like').unbind().click(function(){
$.post("LikeMail.php",
{Like: this.id},
function(data){
$('#response').html(data);
}
);
});
});
viewProfile.php
页
Like
页面中显示Unlike
或viewProfile.php
按钮取决于此代码:
$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="Unlike" value="Unlike" id="Unlike" class="btn btn-lg btn-info edit">';
}
elseif ($numrows_likes == 0) {
echo '<input type="submit" name="Like" value="Like" id="Like" class="btn btn-lg btn-info edit">';
}
按钮取决于上述两个条件
现在,当点击Like
按钮时,来自viewProfile.php
的发布请求来到此处
if(isset($_POST['Like'])) //When Like button in viewProfile.php is clicked then this peace of code inside if condition should run and insert some record in database
{
$total_likes = $total_likes+1;
$like = mysqli_query($conn, "UPDATE user SET user_Likes = '$total_likes' WHERE user_id = '$user2'");
$user_likes = mysqli_query($conn, "INSERT INTO liked (user1,user2) VALUES ('$user1','$user2')");
$query3 = "INSERT INTO notification (user1, user2, alert, notificationType) VALUE ('$user1','$user2','unchecked','like')";
if (mysqli_query($conn, $query3)) {
echo "Like";
} else {
echo mysqli_error($conn);
}
}
同样点击Unlike
按钮时。这种代码的和平应该运行。
if(isset($_POST['Unlike'])) //This is the condition for Unlike button. It should delete record from databse
{
$total_likes = $total_likes-2;
$like = mysqli_query($conn, "UPDATE user SET user_Likes='$total_likes' WHERE user_id='$user2'");
$remove_user = mysqli_query($conn, "DELETE FROM liked WHERE user1='$user1' AND user2='$user2'");
$query3 = "DELETE FROM notification WHERE user1='$user1' AND user2='$user2' AND notificationType='like'";
$check = mysqli_query($conn, $query3);
if ($check) {
echo "Unlike";
} else {
echo mysqli_error($conn);
}
}
的问题:
我遇到的主要问题是,当我点击Like
或Unlike
时,两者都执行Like
按钮代码的条件。两者都将数据插入数据库,因为Unlike
条件应该从数据库中删除数据,但它也会插入数据作为Like
按钮的条件。请你帮我解决一下如何解决这个问题。提前致谢!
更新:
当我删除Like
按钮的所有相应代码时。 Unlike
按钮的条件开始正常工作。
答案 0 :(得分:1)
我认为某处有重复的ID,也许是DIV。看看这个。
<div id="Like_2"></div>
<input type="submit" name="Unlike" value="Unlike" id="Unlike" class="btn btn-lg btn-info edit">
<input type="submit" name="Like" value="Like" id="Like" class="btn btn-lg btn-info edit">
<div id="response"></div>
$(document).ready(function(){
$(document).on('click','#Unlike', function(){
$('#response').html(this.id);
//ajax call
});
$(document).on('click','#Like', function(){
$('#response').html(this.id);
//ajax call
});
});
和javascript函数:
function like()
{
var req = new XMLHttpRequest();
req.onreadystatechange = function()
{
if(req.readyState==4 && req.status==200)
{
document.getElementById('Like_2').innerHTML=req.responseText;
}
}
req.open('POST','LikeMail.php','true');
req.send();
}
setInterval(function(){like()},1000);