We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Passing a Button ID to a VBScript Variable onClick

ADT58
ADT58 asked
on
Medium Priority
1,990 Views
Last Modified: 2012-05-06
Greetings,

I need to pass the ID of a button to a variable when the button is click so that I can store it as a Session Variable and pass it to a page.

Basically I have a table that lists orders. Each order then has a button to "view" the details. I use a tiny bit of javascript to open a seperate page with the details in, which are pulled down into a table via ADO from SQL.

Now what initally did was used the recordset and a select SQL statement to pass the "Order No" to a Session Variable when the button was clicked.

The order No is have I display the details on the "view details" page, not until I tried to test viewing another order which had a different order no did I realise that no matter what I do it will pass the last entered Order No from the record set to the Session Variable hence only ever going to show one Order No.

I thought the best way would be to "onClick" get the ID of the button pressed and since the buttons id's are created in the table using the order no's so "button_<%order no variable%>" I could just capture the id of the button clicked, truncate the final digit(s) and pass them to a session variable?

Is this doable or have I made a completely complicated issue out of something that is simple.

All help and views appreciated :) Oh and I've attached some code, just the code that creates the button (via loop) and the javascript that is used for the new window.

<td class="style2"><div align="center"><input type="button" class="style2" id="view_<% = oRs.Fields("Order No")%>" onclick="window.location.href=('javascript:dopopup();x.focus();');" value="View">
</a> </div></td>
 
<script type="text/javascript">
function dopopup()
{
var chrome= "width=600,"
      + "height=483,"
      + "location=0,"
      + "menubar=0,"
      + "resizable=0,"
      + "scrollbars=0,"
      + "status=0,"
      + "titlebar=0,"
      + "toolbar=0,"
      + "hotkeys=0,"
      + "left=0,"
      + "top=0";
      x=window.open('orderdetails_gfx.asp',null,chrome);
      x.parent.document.title="Order Details";
}
</script>

Open in new window

Comment
Watch Question

Top Expert 2007

Commented:
As an aside, if you don't need a feature for the popup window, there is no need to include, say,

+ "menubar=0,"

because the default for (most) features is "turned off".

You can "reach back" from the popup window to the parent window and (possibly) retrieve the value of the button - or the id of the button - as long as you put the value in a variable declared outside any function:

<td class="style2"><div align="center"><input type="button" class="style2" id="view_<% = oRs.Fields("Order No")%>" onclick="window.location.href=('javascript:dopopup(this);x.focus();');" value="View">

<script type="text/javascript">
 
var button_order = '';
 
function dopopup(button) {
button_order = button.id;
 
// From the popup window:
 
<script type="text/javascript">
var wp = window.parent; // Or "window.opener", I can never remember which :-/
var order_num = wp.button_order;
 
// You will have to strip off the "view_" portion, but this should get you started

Open in new window

Author

Commented:
great but how can I get from a javascript variable to a VBScript variable?

Thanks for your help by the way.
Top Expert 2007

Commented:
Do you mean from the CLIENT to the SERVER?

Author

Commented:
Its all server side with VBScript I only used a tiny bit of java to just popup the window. I need the order no via whichever button was clicked so I can pass it to a variable to be used in a SELECT SQL statement server side.
Top Expert 2007

Commented:
Create a hidden input and set its value to the JavaScript variable before you submit the page.

Author

Commented:
when I did a document.write on the variable all I got was "undefined"

<script type="text/javascript">
var wp = window.opener; // Or "window.opener", I can never remember which :-/
var order_num = wp.button_order;
document.write(order_num);
</script>

That is located in the "popup" window I tried using both opener and parent.

<input type="button" class="style2" id="view_<% = oRs.Fields("Order No")%>" onclick="window.location.href=('javascript:dopopup(this);x.focus();');" value="View">

This is located at the end of the body in the "main" page.

<script type="text/javascript">
 
var button_order = '';
 
function dopopup(button) {
button_order = button.id;
</script>
<script type="text/javascript">

var button_order = '';
 
function dopopup(button)
{
button_order = button.id;

var chrome= "width=600,"
      + "height=483,"
      + "location=0,"
      + "menubar=0,"
      + "resizable=0,"
      + "scrollbars=0,"
      + "status=0,"
      + "titlebar=0,"
      + "toolbar=0,"
      + "hotkeys=0,"
      + "left=0,"
      + "top=0";
      x=window.open('orderdetails_gfx.asp',null,chrome);
      x.parent.document.title="Order Details";
}
</script>

Sorry if I'm useless but I don't have a clue when it comes to Javascript :(

Top Expert 2007

Commented:
Is this just a typo?

<script type="text/javascript">
 
var button_order = '';
 
function dopopup(button) {
button_order = button.id;
</script>
<script type="text/javascript">

var button_order = '';
 
function dopopup(button)
{
button_order = button.id;

Because you don't need the first 7 lines...

Author

Commented:
Sorry type my bad..

Author

Commented:
*typo
Top Expert 2007

Commented:
hmmm...
<script type="text/javascript">
 
var button_order = '';
 
function dopopup(button) {
    button_order = button.id;
    
    var chrome= "width=600,height=483";
    var x = window.open('orderdetails_gfx.asp',null,chrome);
    
//  x.parent.document.title = "Order Details"; // "x.parent"? Isn't this the window.opener??
    
    // I would try:
    
    x.document.title = 'Order Details:' + button.id; // Tack the ORDER# onto the end of the title
    x.document.close(); // VERY important to do this.
}
</script>

Open in new window

Author

Commented:
Tried it, got nothing in the Title bar I'm afraid just had "Mozilla Firefox" for FF and the URL for IE. Really starting to bug me this is :S

I think what I may have to do is just resort to radio buttons with a button on the bottom for Viewing and losing the individual buttons, not the prefered option but may be the only one by the looks.

Any other thoughts at all?
Top Expert 2007

Commented:
Not the title bar, the document title (var dt = document.title;)

Author

Commented:
I'm fniding it difficult to understand what your suggesting Badotz, I have pasted the code you have gave me here there and everywhere and still get no results at all. I'm struggling to understand on the basis of I know nothing at all to do with Javascript.

I think I might be better off just creating radio buttons and having the form post back to the same page and depending on which radio button is selected, opening the window on that basis. I know this can be achieved through VBScript but nothing this way is seeming to work.
Top Expert 2007
Commented:
If you can get the ID of the button into the hidden text field, something you can do in the "onclick" event, then you can add the VALUE of that hidden field to the "document.title" of the new window, as I described in http:#a23634438.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Top Expert 2007

Commented:
So, did it work?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.