473,396 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Onchange Event in Dropdown

Hi Friends,
I have a problem with OnChange event, Please help me out in coding the below requirement.
I have a drop down box with 3 items: India,United States,South Africa. If I select India(for India alone) a pop up window should come up with some text/question("Are you an Indian Citizen?"), Yes/No should be the only options. If the user clicks on Yes, some value should be saved in a variable which i need to save it in my database so as to maintain a record that a question was asked and answered.

Please help me out. Thanks inadvance.

Regards,
Jun 9 '09 #1
41 11212
Dormilich
8,658 Expert Mod 8TB
you could use window.confirm()?
Jun 11 '09 #2
Thanks for the reply.
if the user clicks OK, i want to store some value in a variable and call it in a text box.. How can achieve this?
Jun 11 '09 #3
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script language="javascript">
  4. function ConSub()
  5.   {
  6.    var result;
  7.    var w = document.frm.Country.selectedIndex;
  8.    var selected_text = document.frm.Country.options[w].text;
  9.      if (selected_text == "South Africa")
  10.         'result=window.confirm("Do you have a Emp certificate?");
  11.                  if (result== true)
  12.               // i want to store some value sayYES in a variable and call this variable in a text box//
  13. else 
  14. return false;
  15. }
  16. <form name="frm">
  17. <select name="Country" onchange="return ConSub()">
  18. <option value="India">India
  19. <option value="South Africa">South Africa
  20. </select>
  21. </form>
  22. </body>
  23. </html>
Please edit/comment on how can i track that user has clicked OK or cancel.i want store this in a variable.
Jun 11 '09 #4
waqasahmed996
160 100+
this code may be helpfull for you
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2. function country (value)
  3. {
  4. if(window.confirm("Are You "+value+" citizan"))
  5.      {
  6.      country = value;
  7.       return true;
  8.      }
  9. else
  10.       return false;
  11. }
  12. </script>
  13.  
  14. <select name="country" id="country" onchange="return country(this.value)">
  15. <option value="0">Select Country</option>
  16. <option value="India">India</option>
  17. <option value="United States">United States</option>
  18. <option value="South Africa">South Africa</option>
  19. </select>
  20.  
i need to know how can value of country can be assign to a php variable so that it can be used for insertion in database
Jun 11 '09 #5
Dormilich
8,658 Expert Mod 8TB
@waqasahmed996
  1. submit the form
  2. submit an AJAX request
Jun 11 '09 #6
Thanks again..
The above code will make a cofirm box pop up for every item in the drop down, but i require this function to get fired only when INDIA is selected frm the list.
Not for all the items in the drop down.
Jun 11 '09 #7
waqasahmed996
160 100+
any other way like
Expand|Select|Wrap|Line Numbers
  1.  if(window.confirm("Are You "+value+" citizan"))
  2.       {
  3.     <?php
  4.     $country = echo '<script type="text/JavaScript"> echo value; </script>'; 
  5.     ?>      
  6.  
  7.        return true;
  8.       }
  9.  
Jun 11 '09 #8
I am using ASP pages and javascript, not asp.net or php coding.
Jun 11 '09 #9
Dormilich
8,658 Expert Mod 8TB
that doesn't change the general idea, only the syntax.
Jun 11 '09 #10
I agree... According my script,
var w = document.frm.Country.selectedIndex;
var selected_text = document.frm.Country.options[w].text;
Variable "selected_text" contian the current item selected from the drop down, now i will check a condition (if selected_text=="India"), if yes a confirm box will popup.
now if the user clicks OK/CANCEL, i need to store a true/false value in a varialbe and then assign it to asp variable or a text box. How can i do this
Jun 11 '09 #11
waqasahmed996
160 100+
Dormilich please can you tell me correct syntax. my above code is not executed correctly
Jun 11 '09 #12
Dormilich
8,658 Expert Mod 8TB
I know neither ASP nor ASP.NET
Jun 11 '09 #13
waqasahmed996
160 100+
no i am asking about php and javascript. what is mistake in my following code. where variable 'value' is javascript variable
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  $country = echo '<script type="text/JavaScript"> echo value; </script>'; 
  3.  ?> 
  4.  
Jun 11 '09 #14
Your code executed correctly but the confirm box is popped up for each item clicked in the drop down.. I need this to happen only when i click India not for all... I have no idea how to assign a Javascript variable to a html/asp text box.
Jun 11 '09 #15
Dormilich
8,658 Expert Mod 8TB
@waqasahmed996
echo is not a function and thus cannot be assigned to a variable.
Jun 11 '09 #16
waqasahmed996
160 100+
thanks chandhseke but i am asking from Dormilich
Jun 11 '09 #17
The output of your above code is as follows..
while i select INDIA from drop down, a confirm box comes up with a question: Are you a INDIA citizen( where value is taken as INDIA).. But this is not my concern, my concern ihere s when the user clicks OK or CANCEL then i want this action to be recorded or stored in a variable, and this value should be stored in a text box
Jun 11 '09 #18
acoder
16,027 Expert Mod 8TB
@chandhseke
To assign it to a text box:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("textboxid").value = val;
This would require a submit to be passed to ASP. Another option is to pass it in the URL and change the page immediately:
Expand|Select|Wrap|Line Numbers
  1. location.href = "asppage.asp?val=" + val;
If you want to avoid a page reload, use Ajax.
Jun 11 '09 #19
Dormilich
8,658 Expert Mod 8TB
@chandhseke
what do you mean by "text box"? a <textarea> or a <input> or a <div> element?
Jun 11 '09 #20
waqasahmed996
160 100+
you can write like
document.getElementById('txtbox').innerHTML = 'INDIA'

Dormilich i remove echo from my code even then it is not working
Jun 11 '09 #21
Dormilich
8,658 Expert Mod 8TB
@waqasahmed996
to get output you have to use echo or one of the print() functions
Jun 11 '09 #22
waqasahmed996
160 100+
sorry Dormilich i am still unable to understand. please tell me what should i write exactly

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  $country = echo '<script type="text/JavaScript">  value; </script>'; 
  3.  ?> 
  4.  
Jun 11 '09 #23
Dormilich
8,658 Expert Mod 8TB
I guess
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  echo '<script type="text/JavaScript"> // whatever code </script>'; 
  3.  ?> 
  4.  
will do.

EDIT: if your problem is not related to chandhseke's problem, I'll move the corresponding posts to a new thread in the PHP forum.
Jun 11 '09 #24
acoder
16,027 Expert Mod 8TB
@waqasahmed996
Besides the PHP problem, you're mixing JavaScript and PHP incorrectly. You can use PHP (or any server-side language) to generate JavaScript code during page load, but you can't use this kind of code to run PHP after the page has loaded.
Jun 11 '09 #25
In vbscript, i am using message box If the user clicks OK button.. I want to assing a value to a text box <input>..Please help me out in this

Expand|Select|Wrap|Line Numbers
  1. <Script lang="VBScript>
  2. dim a
  3. dim res= Yes
  4. a=MsgBox("you pressed ok",VbYesNo)
  5.  
  6. if a = VbYes then
  7.  
  8. <i want to assign the variable "res" to a text box below>
  9. </script>
  10.  
  11. <html>
  12. <head><body>
  13. <input type="text" name="test" value=''>
  14. </head></html>
Jun 26 '09 #26
acoder
16,027 Expert Mod 8TB
Two questions:
1. Is this related to the original problem?
2. Why are you using VBScript when JavaScript is more cross-browser friendly?

PS. please use [code] tags when posting code.
Jun 28 '09 #27
As there is NO window object with YES and NO buttons in Javascript, i am making use of both Javascript and VB script..

In javascript i am using selectedIndex to track the selected item from the drop down, within IF conditon i am calling VBscript function(contains a MsgBox) if the result is true...

Now within VBscript i need to write a condition, If the user clicks on Yes button then i want to assign some value to a textbox so that i can save this text box value in the database.
Jun 29 '09 #28
acoder
16,027 Expert Mod 8TB
You do realise that this would restrict your site to IE only? You can phrase your question so that the options for confirm() (OK/Cancel) are suitable. Are you sure you still want to go with VBScript?

If you do, the method will be similar to what has been suggested earlier. Set the value of the text box like this:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("textboxid").value = val;
where "textboxid" is the ID of the text box and val is the value that needs to be assigned.
Jun 29 '09 #29
Thanks for the reply.
But as per the requirement, YES or NO should be the only options..that is the reason i am using VBscript... Anyways i will speak to the requestors regarding the above subject... If i am using VBscript what will be the syntax in VBscript to assign a value to a text box..??
Jun 29 '09 #30
acoder
16,027 Expert Mod 8TB
I don't know if you can use document.getElementById(), but if you add a form element, you can use the following syntax:
Expand|Select|Wrap|Line Numbers
  1. document.formname.fieldname.value = res;
Jun 29 '09 #31
Example : If i add a text box named as text1 to the existing form form1, then the syntax would be :

dim res= 'YES"
Document.form1.text1.value= res...
Jun 29 '09 #32
acoder
16,027 Expert Mod 8TB
Yes, but your page code is a bit messed up, e.g. the <head> tags are in the wrong place and the script is outside the HTML tag. You also need to put your code inside a function to be run when an event takes place, e.g. when a button is clicked.
Jun 29 '09 #33
As suggested i had made the below code changes in my form, when i tested my database it contains Blank value..Please Review the code below and advise

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script language="Javascript">
  4. function fun()
  5. {
  6. var res;
  7. var w=document.form1.SetUpCountry.selectedIndex;
  8. var selected_text=document.form.SetUpCountry.options[w].value;
  9. if (selected_text == "South Africa")
  10. {
  11. return pop()    
  12. }    
  13. else
  14. return false;
  15. }
  16. </script>
  17.  
  18. <script language="VBScript">
  19. function pop()
  20. dim b
  21. dim result 
  22. dim answer
  23. result = "Yes"
  24. answer = "No"
  25. b=msgbox("Do you have a BEE certificate?",vbYesNo,"New Supplier Setup")
  26. if b = vbYes then
  27. document.form1.Text1.value = result
  28. Else 
  29.     document.form1.text1.value = answer    
  30. end if
  31. end function 
  32. </script>
  33. </head>
  34. <title>Supplier Request</title>
  35. <body>
  36. <form name="form1">
  37. ----------------------
  38. ------------------
  39. <select name="SetUpCountry" onchange="fun()">
  40. <option value="India">India</option>
  41. <option value="South Africa">South Africa</option>
  42. <option value="Germany">Germany</option>
  43. </select>
  44. </form>
  45. </body>
  46. </html>
Jun 29 '09 #34
Here is another code snippet, save this code and run it in your browser... A drop down and a text box will be displayed. When you select an item from the drop down list, a MsgBox will pop up and when you click on Yes Button the variable (a =YES) value should come into the text box else if you click on NO button varibale (c = NO) should come into text box...I have coded it as per your suggestion but didnt worked to me.. Please edit the below code... Thanks in advance

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/vbscript">
  4. function fun
  5. dim a 
  6. dim b
  7. dim c 
  8. c = "No"
  9. a = "Yes"
  10. b=MsgBox("Message",VbYesNo)
  11. if b = VbYes then
  12. document.myform.text1.value=a
  13. else
  14. document.myform.text1.value=c
  15. End if
  16. document.write(a)
  17. End function
  18. </script>
  19. <body>
  20. <form name="myform">
  21. <select name="country" onchange="return fun()">
  22. <option value="india">india</option>
  23. <option value="india">SA</option>
  24. </select>
  25. <input type="text" name="text1" value="&nbsp;">
  26. </form>
  27. </body>
  28. </html>
Jun 29 '09 #35
acoder
16,027 Expert Mod 8TB
Three problems I notice:
1. Both options have the same value "india".
2. The 'return' in onchange. Remove it.
3. The document.write() statement which will cause the page to be reopened and overwritten. Remove that line.

PS. please use [code] tags around your code. See How to Ask a Question.
Jun 30 '09 #36
acoder
16,027 Expert Mod 8TB
@chandhseke
To actually make changes to the database, you will need to submit the form and your action page (server-side script) should connect to and update the database.
Jun 30 '09 #37
Hi folks,

Thanks a lot for your help on this..It worked for me.

Many thanks
Jun 30 '09 #38
acoder
16,027 Expert Mod 8TB
No problem. Glad it's now working :)
Jul 1 '09 #39
Many thanks again..

I have one more small question to you, how can i validate the text box value in ASP against a database table (Written using VBScript) before submission of the Online form??

Background: I have an online business form, while a new requestor enters thier ID # it should be validated against a database table to check whether the ID# exists in the database, if NO an error msg should be displayed.

Lets make a long story into small, i am working as a Application Maintanence engineer and this is the new requirement which i havent done before..Please guide me through as you did with last Thread..Many Thanks
Jul 1 '09 #40
acoder
16,027 Expert Mod 8TB
Your question is no longer relevant to this thread, nor to JavaScript. Repost your question in the ASP forum where you will get a better response.
Jul 1 '09 #41
Ok Sorry for that.. Thank you
Jul 1 '09 #42

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Covad | last post by:
Hi all, For some reason my change() function is only called when the page loads. I'd much rather it gets called when the select changes. Here's the code: window.onload = init; function...
10
by: Ryan McGeary | last post by:
In a <select> drop-down, the onchange event isn't called when scrolling through the dropdown using the mouse-wheel and when crossing over a new <optgroup>. Using the example below, notice how...
2
by: Asit | last post by:
In JavaScripts checks for an onChange event against the value of the textbox at the time of the last onChange event. Since an onChange Event never fired after you changed the text first time ,...
4
by: Zeebra3 | last post by:
Here goes: I have a web form with several asp:dropdownlists, with which, when selection is changed I want to fire an event defined in some clientside js. The content of the clientside code is...
3
by: jab3 | last post by:
Hello. I"m new to this group, and to JavaScript in general, so please forgive me if I breach local etiquette. I'm trying to implement some client-side 'dynamic' validation on a form. I'm having...
3
by: b_naick | last post by:
I realize that the onChange event for a drop down can be trapped as follows: <select name="myDropDown" onChange="somefunc"> Is it possible to trap the onChange event outside of the select...
3
by: countocram | last post by:
hi! Im having a problem retaining the inputs on my text fields. When i selected an option from a dropdown list with an onChange=location.. event. What happen is that, everytime I choose an option the...
21
by: Leena P | last post by:
i want to basically take some information for the product and let the user enter the the material required to make this product 1.first page test.php which takes product code and displays...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.