0
<head>  
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
         $(document).ready(function(){
              $("#submit").click(function(){
                     $.get("invite_friends_session.php", function(){
                    });
               });
          });
</script>
</head>
<body>
<form >
<input type="submit" name="submit" id="submit" style="margin-top:100px; margin-left:100px;" />
</form>
</body>

the $.get("invite_friends_session.php") part does not work. But it works if i keep it outside the callback function.

But i need to call the invite_friends_session.php whenever there is a click on the #submit.

how to do that?

1
  • So your .click listener/handler is functioning correctly (you can do other things within it?) and it's only your $.get function that has an issue inside the handler? Commented Jul 11, 2011 at 4:46

3 Answers 3

5

Since the submit button is inside a form, clicking the submit button bubbles up and submits the form. You need to add a return false to the end of the handler:

$("#submit").click(function(){
    $.get("invite_friends_session.php", function(){
    });
    return false;
});
Sign up to request clarification or add additional context in comments.

1 Comment

@The Scrum Meister, ur code didn't work for me. did u run it? or any mistake on my side?
1

[Edit] You should prevent the default form submit action upon click.

$(document).ready(function(){
  $("#submit").click(function(e){
    e.preventDefault(); //edit
    $.get({ url: "invite_friends_session.php",
            success: function() {
              alert('success');
            },
            error: function() {
              alert('error');
            },
            complete: function() {
              alert('complete');
            }
          });
  });
});

5 Comments

-1 According to the Documentation the .get() can be called with the parameters url, success
Concede, that said, it's not a loss to switch to this method since it will be more conducive to debugging. He should add 'error' and 'complete' actions to see what's going on (as well as check in firebug)
@ The Scrum Meister , no, if I use the $.get without any success outside the callback function, it works
@ghayes, where did u use json?
@sof_user, ScrumMeister's suggestion should fix your issue. By default, a click on the submit button will submit your form object. You can avoid this by returning false in your click-handler, or calling e.preventDefault() from it. That said, I would strongly suggest using an <input type="button" id="submit"> for more semtantic clarity.
0

Remove the <form> tags around the submit button. Since you're using jQuery's .get method, you don't need that form tag (technically, it doesn't even need to be a submit input button). Right now, it's trying to post using the form instead of getting the page via jquery.

Example: http://jsfiddle.net/R3CWp/

<head>  
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
         $(document).ready(function(){
              $("#submit").click(function(){
                     alert("i got clicked");
                     $.get("invite_friends_session.php", function(){
                    });
               });
          });
</script>
</head>
<body>
<input type="submit" name="submit" id="submit" style="margin-top:100px; margin-left:100px;" />
</body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.