
-----------------------------------
nonamedude
Thu Apr 14, 2011 9:49 am

Jquery/Php Help(function is running twice)
-----------------------------------
I am working on my project and I decided to include jquery in it so that it can have a rating system. Everything works fine but the function that runs on being clicked is being ran twice. 

This is what I have right now;

[code]$(document).ready(function()
            {
//$('.common').bind('click', function () {
//var myid = $(this).attr('id');
//});
                $('.common').bind('click',function()
                {  
                var myid = $(this).attr('id');
                    var newValue = parseInt(myid) + 10;
                    $( "#" + myid + " img").attr("src","images/thumbup_blue.png");
                    $( "#" + (newValue) + " img").attr("src","images/thumbdown_gray.png");
                 $.get(
                    "http://localhost/gwu/folder/includes/mqsql.php?id="+myid,
                    null,
                    function(data) {
                        var value = data;
						alert (value);
                        
                        // $('#c').replaceWith(value);
                // $( "#" + myid).html(value);
                }
                )
                }
            )
                $('.common2').bind('click',function()
                {
                            var myid = $(this).attr('id');
                            var newmyid = myid -10;
                       $.get(
                    "http://localhost/folder/includes/mqsql2.php?id="+newmyid,
                    null,
                    function(data) {
                        // alert(data);
                        var value2 = data;
                       // alert (value);
                        alert (value2);
                        // $('#c').replaceWith(value);
                     //  $( "#" + myid).html(value);
                }
                )
            
               
                    $("#" + myid + " img").attr("src","images/thumbdown_red.png");
                  $("#" + (myid-10) + " img").attr("src","images/thumbup_gray.png");
                }
            )
            }

        )[/code]

So the piece of code above increments the value of something in the database, and since its running twice, its incrementing it twice o.0
And this is is the code for my images.

 [code]
                                 
                                 
                                
                             0
                                        [/code]

I have no idea why this is happening.

-----------------------------------
2goto1
Thu Apr 14, 2011 10:09 am

RE:Jquery/Php Help(function is running twice)
-----------------------------------
I'm guessing that jQuery event propagation is working its way up the event chain from the image or the div, up to the anchor tag. 

Try narrowing your jQuery selector to $("a.common") so that click will only fire for your anchor links, if that's your intention. Or remove class="common" for your image and div tags, so that the "common" class is only assigned to your anchor tags

-----------------------------------
DemonWasp
Thu Apr 14, 2011 10:58 am

RE:Jquery/Php Help(function is running twice)
-----------------------------------
Alternately, you can stop JQuery from propagating the event internally by modifying your handler:

[code]
$('.common2').bind (
    'click',
    function ( event ) {
        event.stopPropagation();
        // event-handling code here...
    }
);
[/code]

But in general, 2goto1's solution makes more sense. Write clear, obvious code and you can largely eliminate this kind of issue.

You should probably also use relative URLs for your GET operations.

-----------------------------------
nonamedude
Thu Apr 14, 2011 6:34 pm

Re: Jquery/Php Help(function is running twice)
-----------------------------------
Thanks, specifying the anchor made it work :D.

The last problem I have is printing out stuff to my div
Right now, i use

 $( "#" + myid).html(value); 
But that just changes my img and prints out value.

I need it to print out to the div

  0 
                                        

I cant seem to find the code for that :/

-----------------------------------
2goto1
Thu Apr 14, 2011 8:34 pm

RE:Jquery/Php Help(function is running twice)
-----------------------------------
