Luca DeCaprio
Luca DeCaprio

Reputation: 87

Call JSP method (or PHP script) from Javascript?

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...

Upvotes: 0

Views: 1305

Answers (3)

Ali Muzaffar Khan
Ali Muzaffar Khan

Reputation: 143

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/

Upvotes: 1

u.k
u.k

Reputation: 3091

Your options are:

Upvotes: 0

SeanCannon
SeanCannon

Reputation: 78006

$('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
        }
    });
});

Upvotes: 1

Related Questions