jQuery ajax 适用于 Chrome,但不适用于 Firefox 或 IE

发布于 2024-12-12 10:37:25 字数 6300 浏览 0 评论 0原文

公平警告:我不是专家,但我确实做到了这一点。我的代码不漂亮而且很粗糙。这是一个相当复杂的系统,所以不要羞于提出问题。

所以我有一个恼人的问题,我的代码可以在 chrome 中运行,但在其他地方却无法运行。我的 javascript 似乎无法在 Firefox 或 IE 中运行。请注意,每次您在 DIV 中看到 PHP 时,它仅代表数据库中帖子的编号。

我的代码显示帖子,其中每个帖子都与使用跨度构建的喜欢和不喜欢按钮配对。有一个复选框可以显示/隐藏所有喜欢的帖子,另一个复选框可以在选中时对不喜欢的帖子执行相同的操作。当用户通过单击按钮喜欢或不喜欢某个帖子时,值会通过 ajax(发送到 check.php)发送到我的数据库,以便在将来访问时可以调用它们。

同样,在 Chrome 中一切正常,但在 IE 和 Firefox 中则不然。

另外,除非我首先手动将值插入数据库中的 userPosts 表中,否则不会在我的数据库中保存新的帖子和值。例如,如果我的数据库已经具有帖子 1-3 的值,则用户未来喜欢/不喜欢这些帖子的所有决定都会发送并保存,没有问题,但如果我添加新帖子(帖子 4)并且用户喜欢或不喜欢它,没有发送任何值...似乎 INSERT 在 check.php 中不起作用,而 UPDATE 功能很好。

这是位于循环内的 jQuery,您应该会发现它的注释令您满意:

<script type="text/javascript">
$(document).ready(function() {

// Declare variables
    var checked = <?php echo $row['value']; ?>; //get value of Liked or Disliked from database
    var postID = <?php echo $row['postID']; ?>; //get post ID from database
    var userID = <?php echo $current_user->ID; ?>; //get the wordpress user's ID
    var showLikes = $("input[name*='show_likes']"); //represents checkbox for Hide Liked
    var showDislikes = $("input[name*='show_dislikes']"); //represents checkbox for Hide Disliked

// Set the remembered Liked and Disliked buttons
    if (checked == 1) {
        $('#post_<?php echo $row['postID']; ?>').addClass('like'); 
        $('#like_<?php echo $row['postID']; ?>').removeClass('likeimgoff').addClass('likeimgon');
    } else if (checked == 0) {
        $('#post_<?php echo $row['postID']; ?>').addClass('dislike'); 
        $('#dislike_<?php echo $row['postID']; ?>').removeClass('dislikeimgoff').addClass('dislikeimgon');
    }

//When Liked button is clicked do this
$('#like_<?php echo $row['postID']; ?>').click(function() {
// Declare variables
    var value = '1';

// Send values to database
    $.ajax({
        url: 'check.php',
        type: 'POST',
        data: 'userID=' + userID + '&postID=' + postID + '&value=' + value,
        success: function(result) {
            $('#Message_<?php echo $row['postID']; ?>').html('').html(result).prependTo('#post_<?php echo $row['postID']; ?>');
        }
    });

// If post is Disliked, change to Liked
    $('#post_<?php echo $row['postID']; ?>').removeClass('dislike').addClass('like');
    $('#dislike_<?php echo $row['postID']; ?>').removeClass('dislikeimgon').addClass('dislikeimgoff');
    $('#like_<?php echo $row['postID']; ?>').removeClass('likeimgoff').addClass('likeimgon');
// If Hide Liked checkbox is on, toggle the post
    if (showLikes.attr('checked')) {
        $('#post_<?php echo $row['postID']; ?>').toggle();
    }

    return false;
});

//When Disliked button is clicked do this
$('#dislike_<?php echo $row['postID']; ?>').click(function() {
// Declare variables
    var value = '0';
// Send values to database
    $.ajax({
        url: 'check.php',
        type: 'POST',
        data: 'userID=' + userID + '&postID=' + postID + '&value=' + value,
        success: function(result) {
            $('#Message_<?php echo $row['postID']; ?>').html('').html(result).prependTo('#post_<?php echo $row['postID']; ?>');
        }
    });

// If post is Liked, change to Disliked
    $('#post_<?php echo $row['postID']; ?>').removeClass('like').addClass('dislike');
    $('#like_<?php echo $row['postID']; ?>').removeClass('likeimgon').addClass('likeimgoff');
    $('#dislike_<?php echo $row['postID']; ?>').removeClass('dislikeimgoff').addClass('dislikeimgon');
// If Hide Disliked checkbox is on, toggle the post
    if (showDislikes.attr('checked')) {
        $('#post_<?php echo $row['postID']; ?>').toggle();
    }

    return false;
});

//When Hide Liked checkbox clicked, toggle all Liked posts.
    $("input[name*='show_likes']").click(function() {
        if ($('#post_<?php echo $row['postID']; ?>').is('.like')) {
            $('#post_<?php echo $row['postID']; ?>').toggle();
        }
    });

//When Hide Disliked checkbox clicked, toggle all Disliked posts.
    $("input[name*='show_dislikes']").click(function() {
        if ($('#post_<?php echo $row['postID']; ?>').is('.dislike')) {
            $('#post_<?php echo $row['postID']; ?>').toggle();
        }
    });

});
</script>

这是每个帖子的代码,也位于循环中,后跟 ajax 返回 check.php 的输出时出现的 #Message 和最后关闭循环:

<div id="post_<?php echo $row['postID']; ?>" class="post">
<div id="post_<?php echo $row['postID']; ?>_inside" class="inside">
    <div id="like">
        <a id="like_<?php echo $row['postID']; ?>" class="likeimgoff" href="#"><span></span></a>
    </div>
    <div id="dislike">
        <a id="dislike_<?php echo $row['postID']; ?>" class="dislikeimgoff" href="#"><span></span></a>
    </div>
    <b><?php echo $row['Title']; ?></b><br>
    <?php echo $row['Description']; ?><br>
</div>
</div>
<div id="Message_<?php echo $row['postID']; ?>" class="reminder"></div>

<?php 
} 
?>

</div>

这是 check.php:

<?php
mysql_connect("name.database.com", "username", "password") or die(mysql_error());
mysql_select_db("databasename") or die(mysql_error());

if (isset($_POST['userID'])){
$userID = mysql_real_escape_string($_POST['userID']);   
}else{
echo "No userID";
}

if (isset($_POST['postID'])){
$postID = mysql_real_escape_string($_POST['postID']);   
}else{
echo "No postID";
}

if (isset($_POST['value'])){
$value = mysql_real_escape_string($_POST['value']);   
}else{
echo "No value";
}

$query = mysql_query("SELECT * FROM userPosts WHERE userID='$userID' AND postID='$postID';") or die(mysql_error()); 

if (mysql_num_rows($query) > 0) { 
mysql_query("UPDATE userPosts SET value='$value' WHERE userID='$userID' AND postID='$postID';") or die(mysql_error());
} else {
mysql_query("INSERT INTO userPosts (userID, postID, value) VALUES ('$userID', '$postID', '$value') ") or die(mysql_error()); 
} 

echo "UserID: " .$userID. " PostID: " .$postID. " Value: " .$value;
?>

所以你已经有了它。我知道这是很多代码,所以请不要回避并随意提问!

Fair warning: I am no expert, but I did manage to get this far. My code isn't beautiful and it is rough. It is a fairly complex system so don't be shy to ask questions.

So I have an annoying problem where my code works in chrome but nowhere else. It seems like none of my javascript is working in either Firefox or IE. PLEASE NOTE THAT EVERY TIME YOU SEE PHP INSIDE A DIV IT SIMPLY REPRESENTS THE # OF THE POST INSIDE THE DATABASE.

My code displays posts where each post is paired with a like and dislike button built with spans. There is a checkbox that shows/hides all liked posts and another that does the same for disliked posts when selected. When a user likes or dislikes a post by clicking the button, values are sent to my DB through ajax (to check.php) so they can be recalled on future visits.

Again, it all works fine in Chrome but not in IE and Firefox.

Also, unless I insert the values into my userPosts table in my database manually first, no new posts and values are saved in my DB. For example, if my DB already has values for posts 1-3, all future decisions by the user to like/dislike those posts are sent and saved no problem but if I add a new post (post4) and the user likes or dislikes it, no values get sent... it seems like the INSERT doesn't work in check.php whereas the UPDATE functions just fine.

Here is the jQuery that sits inside the loop, you should find it annotated to your satisfaction:

<script type="text/javascript">
$(document).ready(function() {

// Declare variables
    var checked = <?php echo $row['value']; ?>; //get value of Liked or Disliked from database
    var postID = <?php echo $row['postID']; ?>; //get post ID from database
    var userID = <?php echo $current_user->ID; ?>; //get the wordpress user's ID
    var showLikes = $("input[name*='show_likes']"); //represents checkbox for Hide Liked
    var showDislikes = $("input[name*='show_dislikes']"); //represents checkbox for Hide Disliked

// Set the remembered Liked and Disliked buttons
    if (checked == 1) {
        $('#post_<?php echo $row['postID']; ?>').addClass('like'); 
        $('#like_<?php echo $row['postID']; ?>').removeClass('likeimgoff').addClass('likeimgon');
    } else if (checked == 0) {
        $('#post_<?php echo $row['postID']; ?>').addClass('dislike'); 
        $('#dislike_<?php echo $row['postID']; ?>').removeClass('dislikeimgoff').addClass('dislikeimgon');
    }

//When Liked button is clicked do this
$('#like_<?php echo $row['postID']; ?>').click(function() {
// Declare variables
    var value = '1';

// Send values to database
    $.ajax({
        url: 'check.php',
        type: 'POST',
        data: 'userID=' + userID + '&postID=' + postID + '&value=' + value,
        success: function(result) {
            $('#Message_<?php echo $row['postID']; ?>').html('').html(result).prependTo('#post_<?php echo $row['postID']; ?>');
        }
    });

// If post is Disliked, change to Liked
    $('#post_<?php echo $row['postID']; ?>').removeClass('dislike').addClass('like');
    $('#dislike_<?php echo $row['postID']; ?>').removeClass('dislikeimgon').addClass('dislikeimgoff');
    $('#like_<?php echo $row['postID']; ?>').removeClass('likeimgoff').addClass('likeimgon');
// If Hide Liked checkbox is on, toggle the post
    if (showLikes.attr('checked')) {
        $('#post_<?php echo $row['postID']; ?>').toggle();
    }

    return false;
});

//When Disliked button is clicked do this
$('#dislike_<?php echo $row['postID']; ?>').click(function() {
// Declare variables
    var value = '0';
// Send values to database
    $.ajax({
        url: 'check.php',
        type: 'POST',
        data: 'userID=' + userID + '&postID=' + postID + '&value=' + value,
        success: function(result) {
            $('#Message_<?php echo $row['postID']; ?>').html('').html(result).prependTo('#post_<?php echo $row['postID']; ?>');
        }
    });

// If post is Liked, change to Disliked
    $('#post_<?php echo $row['postID']; ?>').removeClass('like').addClass('dislike');
    $('#like_<?php echo $row['postID']; ?>').removeClass('likeimgon').addClass('likeimgoff');
    $('#dislike_<?php echo $row['postID']; ?>').removeClass('dislikeimgoff').addClass('dislikeimgon');
// If Hide Disliked checkbox is on, toggle the post
    if (showDislikes.attr('checked')) {
        $('#post_<?php echo $row['postID']; ?>').toggle();
    }

    return false;
});

//When Hide Liked checkbox clicked, toggle all Liked posts.
    $("input[name*='show_likes']").click(function() {
        if ($('#post_<?php echo $row['postID']; ?>').is('.like')) {
            $('#post_<?php echo $row['postID']; ?>').toggle();
        }
    });

//When Hide Disliked checkbox clicked, toggle all Disliked posts.
    $("input[name*='show_dislikes']").click(function() {
        if ($('#post_<?php echo $row['postID']; ?>').is('.dislike')) {
            $('#post_<?php echo $row['postID']; ?>').toggle();
        }
    });

});
</script>

Here is the code for each post, also sitting in the loop followed by the #Message that appears when the ajax returns the output of check.php and finally closes the loop:

<div id="post_<?php echo $row['postID']; ?>" class="post">
<div id="post_<?php echo $row['postID']; ?>_inside" class="inside">
    <div id="like">
        <a id="like_<?php echo $row['postID']; ?>" class="likeimgoff" href="#"><span></span></a>
    </div>
    <div id="dislike">
        <a id="dislike_<?php echo $row['postID']; ?>" class="dislikeimgoff" href="#"><span></span></a>
    </div>
    <b><?php echo $row['Title']; ?></b><br>
    <?php echo $row['Description']; ?><br>
</div>
</div>
<div id="Message_<?php echo $row['postID']; ?>" class="reminder"></div>

<?php 
} 
?>

</div>

Here is check.php:

<?php
mysql_connect("name.database.com", "username", "password") or die(mysql_error());
mysql_select_db("databasename") or die(mysql_error());

if (isset($_POST['userID'])){
$userID = mysql_real_escape_string($_POST['userID']);   
}else{
echo "No userID";
}

if (isset($_POST['postID'])){
$postID = mysql_real_escape_string($_POST['postID']);   
}else{
echo "No postID";
}

if (isset($_POST['value'])){
$value = mysql_real_escape_string($_POST['value']);   
}else{
echo "No value";
}

$query = mysql_query("SELECT * FROM userPosts WHERE userID='$userID' AND postID='$postID';") or die(mysql_error()); 

if (mysql_num_rows($query) > 0) { 
mysql_query("UPDATE userPosts SET value='$value' WHERE userID='$userID' AND postID='$postID';") or die(mysql_error());
} else {
mysql_query("INSERT INTO userPosts (userID, postID, value) VALUES ('$userID', '$postID', '$value') ") or die(mysql_error()); 
} 

echo "UserID: " .$userID. " PostID: " .$postID. " Value: " .$value;
?>

So there you have it. I know it is a lot of code, so please don't shy away and feel free to ask questions!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅沫记忆 2024-12-19 10:37:26

抱歉,我没有花时间阅读您的整篇文章,但通常此类问题是 JavaScript 错误。如果没有,请在 Firefox 上安装 Firebug,然后在 Firebug 控制台中查看是否有任何错误,同时查看是否进行了 ajax 调用以及得到什么答案。

Sorry, I didn't took the time to read your whole post but usually this type of problems are of a javascript error. If you don't have it, install Firebug on Firefox and see in the firebug console if you have any errors, also look if the ajax call is made and what answer you get.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文