Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 9th, 2004, 11:10 PM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default Set form value equal to databse

Is it possible to set a checkbox, radio button or a select menu option equal to data pulled from a database? For examply, if I have something that has a boolean value of Yes, can I set my checkbox, radio or select menu to yes based upon that database? So if it were no, then the on screen representation would be no?

Clay Hess
__________________
Clay Hess
 
Old May 9th, 2004, 11:24 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Yes, this is very much possible.
However it is more likely that you will use your Server Side language to do most of this.
What language are you using for your server side code?

Basically in HTML you need to provide the keyword checked to a radio or checkbox you wish to be checked

An example in Classic ASP would be

<input type="checkbox" name="Test1" value="A" <% if yourBooleanVariable then response.write(" CHECKED ")%>>

This code depends on the language you are using.


======================================
They say, best men are moulded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old May 19th, 2004, 10:26 AM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is actually going to be client side not server side. So...with that in mind, can anyone tell me if they have set a select menu initial value selected equal to say a value in a cookie or something like that?

Clay Hess
 
Old May 19th, 2004, 10:41 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

So you want to search the options of a <select> menu and if one of them has the same value of your cookie select it?

Snib

<><
 
Old May 19th, 2004, 12:14 PM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sort of...let me expound because it is a unique situation...

I have a backend unix server. I have some .vbs files that allow me to communicate with certain aspects on the unix box.

I have a form wherein a user can select yes or no to certain options and then they can click submit and those option values update the unix box. I want to set the selected item on my yes/no select menu equal to what the value is set to currently on the unix box. I can pull what the value is and I can set a selected value obviously, but I cannot figure out how to put javascript in the selected box to get it to be dynamic. So if the option is Yes on the unix box, then it is initially yes on my form and the same goes with No.

Quote:
quote:Originally posted by Snib
 So you want to search the options of a <select> menu and if one of them has the same value of your cookie select it?

Snib

<><
Clay Hess
 
Old May 19th, 2004, 12:34 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Can you get the value from VBScript to JS?

If not, good luck :-)

If so, you'll probably want something like this:

/***************function setChecked
cb: the <select> dropdown that has the options yes and no
the <select> should have yes as first selection and no as second
\***************/
function setChecked(yn)
{
//yesno is the value you got from VBScript
if(yesno == 'yes')
document.getElementById(yn).selectedIndex = 0;
else
document.getElementById(yn).selectedIndex = 1;
}
onload = setChecked("the id of the <select> you want");
//example:
onload = setChecked('sel1');


Modify the above code and maybe you can get something from it.

HTH,

Snib

<><
 
Old May 19th, 2004, 01:09 PM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have just a couple of questions...

1) what does the getElementById('yn') do?
2) Why are you setting the selected index to either 0 or 1?

Quote:
quote:Originally posted by Snib
 Can you get the value from VBScript to JS?

If not, good luck :-)

If so, you'll probably want something like this:

/***************function setChecked
cb: the <select> dropdown that has the options yes and no
the <select> should have yes as first selection and no as second
\***************/
function setChecked(yn)
{
//yesno is the value you got from VBScript
if(yesno == 'yes')
document.getElementById('yn').selectedIndex = 0;
else
document.getElementById('yn').selectedIndex = 1;
}
onload = setChecked("the id of the <select> you want");
//example:
onload = setChecked('sel1');


Modify the above code and maybe you can get something from it.

HTH,

Snib

<><
Clay Hess
 
Old May 19th, 2004, 01:14 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Oops, I messed up.

getElementById('yn')

should have been:

getElementById(yn)

and now it is. :D

I am assuming that when I set the selectedIndex to 0, it selects 'Yes', and 1 is 'No'. Just make sure that the first option is Yes, and the second is No (you can reverse this with some slight modifications).

Snib

<><
 
Old May 19th, 2004, 01:28 PM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Snib,

Thanks for the clarification. Let me tell you what I did and maybe you can point me in the right direction because you have in the past (thanks by the way)...Here is what I wrote...

function setChecked(selvalue)
{
 if(ID[0][2] == 'Y')
 document.getElementById(selvalue).selectedIndex = 0;
 else
 document.getElementById(selvalue).selectedIndex = 1;
}
onload = setChecked('opt3308');

Let me explain some of the above...
1) ID[0][2] is an array that is populated with values from the vbs file. This is either a Y or a N
2)The ID of my select menu is opt3308

I am getting the message that document.getElementById(...) is null or not an object. Using an alert, I confirmed that it is null. What are your thoughts?

Quote:
quote:Originally posted by Snib
 Oops, I messed up.

getElementById('yn')

should have been:

getElementById(yn)

and now it is. :D

I am assuming that when I set the selectedIndex to 0, it selects 'Yes', and 1 is 'No'. Just make sure that the first option is Yes, and the second is No (you can reverse this with some slight modifications).

Snib

<><
Clay Hess
 
Old May 19th, 2004, 01:33 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Strange.... in the past I've found a few reasons for this:

1) your browser doesn't support getElementById()
2) getElementById() is called before the element is loaded
3) typo

Can I see the code of your <select> menu? There may be a typo or something there. The code you posted looks fine.

HTH,

Snib

<><





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to set a form as startup form in vb.net 2.0 mallikalapati .NET Framework 2.0 1 February 21st, 2008 09:19 AM
set password equal to variable fizzerchris SQL Server 2005 3 April 30th, 2007 01:45 AM
set form on subform gigi79 Access 2 May 23rd, 2006 03:08 PM
Set Focus on a Sub Form Brendan Bartley Access 5 September 9th, 2005 10:49 AM
How to "Set" a function equal to a DataReader DittoToo ASP.NET 1.0 and 1.1 Basics 1 July 1st, 2003 10:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.