Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6649399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:46:10+00:00 2026-05-26T00:46:10+00:00

My code pulls posts from mysql. Each post has a pair of radio buttons

  • 0

My code pulls posts from mysql. Each post has a pair of radio buttons (like/dislike) that on click, assigns the appropriate class to the div it is attached to and stores the value in mysql (so when the page loads, checked values are recalled). The page has master control checkboxes that when clicked, show/hide all divs that match the appropriate class.

For example: If the user decides to hide all disliked posts, the posts remaining are the liked posts and those that haven’t been decided on. This is fine. The problem however, is in the situation where the user decides to change a liked post into a disliked post (or makes a decision to dislike an undecided post). In that situation, the newly disliked post doesn’t disappear as it should (the switch to hide disliked posts is ON).

The changed values are indeed sent to the database so it isn’t my ajax that is the problem.

I have css to change the background color of my divs based on class and when the user changes his selection, the background changes so I know that the div class is also being changed and as such, that isn’t the problem either.

This leaves the problem lying in the way the checkboxes look for the div classes.

Here’s my code:

<form name="myform" action="" method="post">
    <fieldset>
        <legend>Master Controls</legend>
    <div class="left">
            <p><input id="show_likes" name="show_likes" type="checkbox" value="1" />        
    <label for="b1">Hide Likes:</label></p>
</div>
<div class="right">
    <p><input id="show_dislikes" name="show_dislikes" type="checkbox" value="1" />
    <label for="b1">Hide Disikes:</label></p>
</div>
    </fieldset>
<br><br>

   <fieldset>
        <legend>Posts</legend>

<?php
$data = mysql_query("SELECT * FROM Contests");

while($row = mysql_fetch_array( $data )){
$query = mysql_query("SELECT * FROM userContests WHERE userID='$userID' AND contestID='$row[contestID]';") or die(mysql_error()); 
$checked = mysql_fetch_assoc($query);
?>

<script type="text/javascript">
$(document).ready(function() {
    var checked = <?php echo $checked['value']; ?>;

    if (checked == 1) {
        $('#contest_<?php echo $row['contestID']; ?>').addClass('like'); 
    } else if (checked == 0) {
        $('#contest_<?php echo $row['contestID']; ?>').addClass('dislike'); 
    }

    $("input[name*='pref_<?php echo $row['contestID']; ?>']").click(function() {
        var contestID = <?php echo $row['contestID']; ?>;
        var value = $(this).val();
        var userID = <?php echo $current_user->ID; ?>;

        $.ajax({
            url: 'check.php',
            type: 'POST',
            data: 'userID=' + userID + '&contestID=' + contestID + '&value=' + value,
            success: function(result) {
                $('#Message_<?php echo $row['contestID']; ?>').html('').html(result);
            }
        });

        if (value == 1) {
            $('#contest_<?php echo $row['contestID']; ?>').removeClass('dislike').addClass('like'); 
        } else if (value == 0) {
            $('#contest_<?php echo $row['contestID']; ?>').removeClass('like').addClass('dislike'); 
        }
    });

    $("input[name*='show_likes']").click(function() {
        if ($('#contest_<?php echo $row['contestID']; ?>').is('.like')) {
            $('#contest_<?php echo $row['contestID']; ?>').toggle();
        }
    });

    $("input[name*='show_dislikes']").click(function() {
        if ($('#contest_<?php echo $row['contestID']; ?>').is('.dislike')) {
            $('#contest_<?php echo $row['contestID']; ?>').toggle();
        }
    });
});
</script>


<div id="contest_<?php echo $row['contestID']; ?>" class="post">
<div id="contest_<?php echo $row['contestID']; ?>_inside">
    <b><?php echo $row['Title']; ?></b><br>
    Expires: <?php echo $row['Exp']; ?><br>
    <ul id="listM"></ul>

    <form id="form_<?php echo $row['contestID']; ?>" action="/">  
        <fieldset> 
            <div class="left"><p><input id="like_<?php echo $row['contestID']; ?>" type="radio" name="pref_<?php echo $row['contestID']; ?>" value="1"<?php if ($checked['value'] == "1") echo " checked"; ?> />
            <label for="like_<?php echo $row['contestID']; ?>">Like</label></p></div>
            <div class="right"><p><input id="dislike_<?php echo $row['contestID']; ?>" type="radio" name="pref_<?php echo $row['contestID']; ?>" value="0"<? if ($checked['value'] == "0") echo " checked"; ?> />
            <label for="dislike_<?php echo $row['contestID']; ?>">Dislike</label></p></div>
                <hr />
        </fieldset>  
    </form>  
</div>
</div>
<div id="Message_<?php echo $row['contestID']; ?>"></div>

<?php 
} 
?>

</div>

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:46:11+00:00Added an answer on May 26, 2026 at 12:46 am

    You need to retrigger the hide show logic on form value changes. i took a stab at it. when a preference radio value is changed, it alters the closest ".post"’s class to be liked or disliked respectively and then checks to see if the ".post" should be visible.

    code

    <script type="text/javascript">
    $(document).ready(function() {
        var checked = <?php echo $checked['value']; ?>;
    
        if (checked == 1) {
            $('#contest_<?php echo $row['contestID']; ?>').addClass('like'); 
        } else if (checked == 0) {
            $('#contest_<?php echo $row['contestID']; ?>').addClass('dislike'); 
        }
    
        $("input[name*='pref_<?php echo $row['contestID']; ?>']").click(function() {
            var contestID = <?php echo $row['contestID']; ?>;
            var value = $(this).val();
            var userID = <?php echo $current_user->ID; ?>;
    
            $.ajax({
                url: 'check.php',
                type: 'POST',
                data: 'userID=' + userID + '&contestID=' + contestID + '&value=' + value,
                success: function(result) {
                    $('#Message_<?php echo $row['contestID']; ?>').html('').html(result);
                }
            });
    
            if (value == 1) {
                $('#contest_<?php echo $row['contestID']; ?>').removeClass('dislike').addClass('like'); 
            } else if (value == 0) {
                $('#contest_<?php echo $row['contestID']; ?>').removeClass('like').addClass('dislike'); 
            }
        });
    
        $("input[name*='show_likes']").click(function() {
            if ($('#contest_<?php echo $row['contestID']; ?>').is('.like')) {
                $('#contest_<?php echo $row['contestID']; ?>').toggle();
            }
        });
    
        $("input[name*='show_dislikes']").click(function() {
            if ($('#contest_<?php echo $row['contestID']; ?>').is('.dislike')) {
                $('#contest_<?php echo $row['contestID']; ?>').toggle();
            }
        });
    $("input:radio[name^='pref_']").bind("shouldHide", function(event){
      var dislikesHidden = $("#show_dislikes").prop("checked");
      var likesHidden = $("#show_likes").prop("checked");
      var closestPost = $(this).closest(".post");
      if((dislikesHidden  && closestPost .is(".dislike")) ||
    likesHidden && closestPost.is(".like")
    ){
          closestPost.hide();
      }
    }).change(function(event){
    //if it has a class in the list, it removes it. If it does not have a class in the list, it adds it.
      $(this).closest(".post").toggleClass("like dislike");
      $(this).trigger("shouldHide");
    });
    });
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page (index.php) that pulls posts from mysql database. The posts are
I am writing a Windows service that pulls messages from an MSMQ and posts
I currently have some code that pulls down a list of users in a
I have a code snippet written in PHP that pulls a block of text
I am working on a project that pulls data from JMS queue using PHP
I manage a website that pulls data in from an XML file, the file
I have code that takes the name of a term and pulls in a
Partial Code: My below code pulls a query from my DB and then uses
So I have a PHP backend that pulls some data from SQL, let's just
I'm using the following piece of code to pull a stock price from yahoo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.