0

In my .jsp code, i have to update database with updated variables.

For that i should use jquery variable value in prepared statement,

When i am assigning jquery variable in java variable but am getting NULL. is it possible?

var d1 =$('#'+this.id+'t').val();

I have to use this d1 value in sql query how?

Neelima
  • 27
  • 1
  • 6
  • possible duplicate of [Reference: Why does the PHP (or other server side) code in my Javascript not work?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side-code-in-my-javascript-not-wor) – Quentin Aug 20 '13 at 13:27

2 Answers2

0

You need to send that variable using ajax.

$.get('your.jsp', {data: $('#'+this.id+'t').val()}, function(response) {
    console.log(response);
});
jcubic
  • 61,973
  • 54
  • 229
  • 402
  • thankyou..actually my query is like <% PreparedStatement ps2=con2.prepareStatement("update images set likcnt=likcnt+1 where imgurl=?");%> I have to set d1 variable in the place of ?, how can i setString(1,) that parameter with ajax call, I am new to ajax. can u give clear explanation? – Neelima Aug 20 '13 at 16:33
  • You need to get `GET` http data `data` somehow, I don't know JSP so I can't help, but this code will make request the same as if you post a form or if you type `your.jsp?data=` in address bar. – jcubic Aug 20 '13 at 19:19
  • Found it, `statement.setString(1, request.getParameter("data"));` – jcubic Aug 20 '13 at 19:21
  • in above comment statement.setString(1, request.getParameter("data")); actually instead of "data", i have to use javascript variable so how can it is possible? – Neelima Aug 21 '13 at 06:07
  • @Neelima no you need to use `data` because you send that using ajax `{data: $('#'+this.id+'t').val()}` - `data` is the name of the GET parameter. – jcubic Aug 21 '13 at 07:09
0

JSP code runs on the server when the request from the browser is being handled, and generates the HTML for the page to return to the browser. jQuery is just JavaScript, which runs in the browser, so the two are entirely separate. The JSP has already run in its entirety well before the JavaScript/jQuery code even reaches the browser.

What you'll need to do is write server-side code to store values in your database, then use an AJAX request to send the values to that action. jQuery has a number of functions that simplify AJAX requests, though the base one is the jQuery.ajax() function. Your call would look something like this:

$.ajax({
    url: 'yourPage.jsp',
    data: {
        newValue: d1
    },
    type: 'post',
    success: function(returnedData) {
         // do something with the server response
    }
});
Anthony Grist
  • 38,173
  • 8
  • 62
  • 76