0

Is it possible to call one of my JSP methods from a Javascript Onclick event?

I don't want my user to leave the current page. I need the javascript to call the method, which then runs the SQL update to increment in the database. I read somewhere I could use a hidden Iframe to accomplish this as well...

1

3 Answers 3

1
$('myElement').click(function(){
    $.ajax({
        url: '/path/to/php/file.php?method=foo',
        success: function(data){
            // do something with data, which is the output of your php file
        }
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

Your selector needs looking at
I'm not really sure how to apply this... here is my js code: function feedVote(postId, vote){ if(vote == "1") { document.getElementById('voteUpSpan' + postId).innerHTML = "Thank you for voting!"; document.getElementById('voteDownSpan' + postId).innerHTML = ""; } else{ document.getElementById('voteDownSpan' + postId).innerHTML = "Thank you for voting!"; document.getElementById('voteUpSpan' + postId).innerHTML = ""; } }
And this is what it is referring too: <a href='#' id='voteUp" + results.getString("id") + "' onclick='feedVote(" + results.getString("id") + ", 1)';>
1

Yes you can use hidden iFrame to do this but in my opinion would be to use AJAX. If you'r using jQuery it is pretty straight forward. Just invoke an ajax request on click.

No idea how to do AJAX using jQuery? I am also pasting the links. Pretty straight forward.

http://api.jquery.com/jQuery.post/

http://api.jquery.com/jQuery.get/

http://api.jquery.com/jQuery.ajax/

Comments

0

Your options are:

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.