Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some javascript in which i am running, see below. When i select the button, the item is being removed which is what i want. Then page refreshes and it comes back!

Can anyone help me here??

What I have tried:

javascript:
$(document).ready(function () {
        // When you click on a button = when a challenge is over
        $("input").click(function () {

            // 1. You hide him and mark him as finished
            var $challenge_div = $(this).parent().parent();
            $challenge_div.data("finish", "true");
            $challenge_div.removeClass("show").addClass("hide");

            // 2. You display a challenge that is hide and not finish

            // 2.1. Select all the hide div
            var $challenge_list = $("div[class='hide']");

            // 2.2. Check if the hide div is already finish or not
            $.each($challenge_list, function () {
                var new_challenge = $(this);


                // 2.3. If you find a challenge hide and not finish : display it and stop the loop
                if (new_challenge.data("finish") == false) {
                    new_challenge.removeClass("hide").addClass("show");
                    return true;
                }
            });

            // 3. If after this you have nothing to show = all challenge are finished : I display a message
            if ($("div[class='show']").length == 0) {
                $("#message p").html("You finished all your challenges !");
            }

        });
    });


PHP:

$cnt=0;
    while($dbRow=$dbQuery->fetch(PDO::FETCH_ASSOC)) {
    if($cnt < 3 ) 
    $class = "show"; 
    else 
    $class= 'hide';

                echo "<div id=".$dbRow["ID"]." data-finish='false' <div class=".$class."><h4>".$dbRow["Name"].
                <form method='POST'><input type='hidden' name='ID' value= '".$dbRow['']." '>
                <input type='submit' name=".$dbRow["ID"]." value='Complete' >id=".$dbRow["ID"].
                "</form></div>";
          $cnt++;
Posted
Updated 10-Apr-18 2:32am

1 solution

Remove the type='submit' part of your button - that's causing the page to postback (you're submitting the form) and resetting your page back to how it was when you started.

You could try changing it to a instead, like this:
HTML
<button type="button" name=".$dbRow["ID"]." value='Complete' />
 
Share this answer
 
Comments
Member 13637584 10-Apr-18 9:52am    
I tried this, and it still isnt working? @SuperSuperman
[no name] 10-Apr-18 9:54am    
Are you saving the changes in your database when they're posted back in from the form?
Member 13637584 10-Apr-18 9:56am    
No it hasn’t been saved to the database?
[no name] 10-Apr-18 9:59am    
Aren't you loading it from your database (or other source) using this: while($dbRow=$dbQuery->fetch(PDO::FETCH_ASSOC)) ?
Member 13637584 10-Apr-18 10:06am    
yeah, thats displaying the items but javascript is what i need to stay? The javascript is whats getting rid of it and hiding/showing it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900