• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

using JSP variable in java script

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

i am working ona JSP project where we have a DELETE button. onlick of this button i am calling a java script function like "are you sure you want to delete this user." now i want that the user name should be there in message box like "are you sure you want to delete SACHIN". i have this value (user name) in a java string variable like user_name.

now can someone please guide me how i can put user_name with the message. i try to do this



but my java script stop responding in this case. can someone please help me out!!

regards,

Sachin.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you populating the 'user_name' variable on the server?

Have you viewed the HTML source from your browser to see what is actually coming from the server?
 
sachin yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for bringing my attention on this issue. in my senerio i have foe example 10 recordes as userid and user name and in fron of every button i have an image button like ---



now in each row i have this image and the value attribute consists the user_name. i can pick it from there. but i am not user how to use it with java script. should i use hidden fields?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You didn't answer either of my questions.

How are you populating the JSP variables on the server?
Have you viewed the HTML source from the browser to see if it's being written to the output stream correctly?
 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
var result=window.confirm("Are you SURE you want to delete <%=user_name%>");

Hi,
try this :


var result=window.confirm("Are you SURE you want to delete"+ <%=user_name%>);

OR

put the following code in script function

user_name = <%=user_name%>
var result=window.confirm("Are you SURE you want to delete"+ user_name);
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by N Chaurasia:

try this :

var result=window.confirm("Are you SURE you want to delete"+ <%=user_name%>);
...



N Chaurasia,
Are you sure this is what you meant to post?
 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya,

try the above code, it works fine


I tried the following with my application



select OK / Cancel and the result is as expected.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will not work in the context of the original question. The string will appear outside of the Javascript string literal and be interpreted as a Javascript variable name or cause Javascript syntax errors depending upon what the content of the string variable.
[ July 17, 2006: Message edited by: Bear Bibeault ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This:


Will also cause problems.
You are missing both the quotes for the literal and the end of line character ";".

It should be:


If you are going to post code examples to help beginners, it is important that take the time to proof read it (or better, test it by running it) before sending. Otherwise, you may be doing more harm than good.
[ July 17, 2006: Message edited by: Ben Souther ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[B] hi Ben,
i am using



in my jsp file and it is not causing any problem...





Yes, you're assigning a numeric value to a javascript varible.

In you example code, you've attempted to do the same thing with strings but have't taken the time to quote them properly.
Create a test JSP page that does this and see what happens.


var result=window.confirm("Are you SURE you want to delete"+ <%=user_name%>);
user_name = <%=user_name%>


[ July 17, 2006: Message edited by: Ben Souther ]
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot reference java variables from javascript directly.
You can however generate javascript from your java/jsp code.

You indicated that you had a list of users, and you could delete any one.
That means you need to include the user name with each line.

Something like this might work

<script>
function confirmDel(userName){
var result=window.confirm("Are you SURE you want to delete " + userName);
return result;
}
</script>

Then somewhere down in a loop

<c:forEach var="user" items="${userList}">
<tr>
<td>${user.name}"</td>
<td><input type="image" name="del_image" on[/i]click="return confirmDel('${user.name}');"> </td>
</c:forEach>

So each line/button generated customizes itself by passing a parameter up to the delete function.
Presumably if you pressed "yes", then the page would submit, and you can determine the id of the user whose delete button you clicked.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic