File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Sounds like simple issue but failing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Sounds like simple issue but failing" Watch "Sounds like simple issue but failing" New topic
Author

Sounds like simple issue but failing

Bhanu S Chatta
Greenhorn

Joined: Apr 15, 2006
Posts: 19
Hi,

I hava a form with button, text boxes and list box. Initailly list box is empty and when the button is clicked the data is populated in the list. in the onclick event I am capturing the index of the item that is selected in the javascript and saving that index value in the global variable (index) of the java script. till now no issue. When I click the button again, it will actuallty searches for data in the database and form will be submitted and few other java script functions will be invoked.

My question is how to get hold the state of the variable 'index' in other javascript functions, while form is re-submitted? can I keep the variable 'index' in Session in javascript (I think it's not possible), what are the other options available?

Thanks,
bhanu
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

No. There is no session in JavaScript.

You'll need to submit the value with the data to the server, and send it back on the next page.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bhanu S Chatta
Greenhorn

Joined: Apr 15, 2006
Posts: 19
How can I submit the value to the server from javasctipt? Can you be little more detail? I appreciate that.

Thanks,
bhanu
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

If you are submitting a form, create a hidden input to hold the value.
Bhanu S Chatta
Greenhorn

Joined: Apr 15, 2006
Posts: 19
Bear Bibeault wrote:If you are submitting a form, create a hidden input to hold the value.


Here is more specific detail, I am already using the hidden input to get hold of the state of the variable. But I am missing something.


in Javascript a variable is declared as - var index;

in the onclick event of list I am calling getslectedEnvelope():
function getSelectedEnvelope(){
if(document.form.list.selectedIndex > -1){
index=document.form.list.selectedIndex;
}
}

In the JSP page:

when the button is clicked, sub(btn, form) is invoked in which " form.submit();" is invoked.
And also in the JSP page I am using below hidden input:
<input type="hidden" name="index" value="<%=request.getParameter("index")%>">

When the form loaded oafter submit, the another java script function init() is called and inwhich I want to use the value of the index variable, so that list box selection is preserved. But that index variable is coming out as undefined in the init method.

Please help me where I am going wrong.
Thanks,
Bhanu


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Is all that you are tying to do is to set the select element back to its submitted value?

If so, you are way over-complicating the whole thing.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
index=document.form.list.selectedIndex;

That is setting a variable named index, you are not setting the value of your hidden field.

Why are you not setting the initial value from the server to begin with?

Eric
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
I guess I should finish my posts before I talk to co-workers so I can beat Bear.

Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

But more to the point, if all you want to do is to set the select element back to its submitted value, you don't need the index at all, so passing it around is useless.

Please verify if that's what your purpose is or not.
Bhanu S Chatta
Greenhorn

Joined: Apr 15, 2006
Posts: 19
That is setting a variable named index, you are not setting the value of your hidden field.

Why are you not setting the initial value from the server to begin with?



yes, All I am trying to do is to make the item as selected after submit also . now can you tell me where I am doing wrong?? How to set the value for hidden field in the javascript function?
In the form object, while debugging I am not getting the list items or the item that is selected.

Thanks,
bhanu
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Bhanu S Chatta wrote:yes, All I am trying to do is to make the item as selected after submit also .

OK, that's easy.

How to set the value for hidden field in the javascript function?

You don't need to! The only info you need is the value of the select that is submitted. No extra info is needed.

When you are setting up the dropdown in the JSP, simply set the selected="selected" attribute on the <option> element that matches the submitted value.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sounds like simple issue but failing