KeySi
KeySi

Reputation: 61

jsp oracle with javascript function

The problem is Code is working when rs.getString (1) is integer number, but in my case it's a varchar primary key , how to fix this.

function getId(id){
    var f=document.form;
    f.method="post";
    f.action='SelectBank1.jsp?id='+id;
    f.submit();
}

<%
Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("select IdwhichIsVarchar, description from  SomeDataBase where IS_VISIBLE = 1 order by description  ");
%>

<%
while(rs.next()){
%>
<tr><td><%=rs.getString(2)%></td>

<td><input type="button" name="edit" value="Edit" style="background-color:green;font-weight:bold;color:white;" onclick="getId(<%=rs.getString(1)%>);" ></td>
</tr>
<%
}
%>

Upvotes: 3

Views: 120

Answers (1)

Shadow Wizard
Shadow Wizard

Reputation: 66389

You need to wrap the value with quotes:

onclick="getId('<%=rs.getString(1)%>')"

Otherwise you will get such output for example:

onclick="getId(foo)"

And JavaScript will try to parse "foo" as variable thus fail. With the fix it will be:

onclick="getId('foo')"

And "foo" will be passed as a string to the function.

Upvotes: 1

Related Questions