Connecting Tech Pros Worldwide Forums | Help | Site Map

Onchange Event in Dropdown

Member
 
Join Date: Jun 2009
Posts: 37
#1: Jun 9 '09
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,

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,652
#2: Jun 11 '09

re: Onchange Event in Dropdown


you could use window.confirm()?
Member
 
Join Date: Jun 2009
Posts: 37
#3: Jun 11 '09

re: Onchange Event in Dropdown


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?
Member
 
Join Date: Jun 2009
Posts: 37
#4: Jun 11 '09

re: Onchange Event in Dropdown


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.
Member
 
Join Date: Jun 2008
Posts: 97
#5: Jun 11 '09

re: Onchange Event in Dropdown


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
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,652
#6: Jun 11 '09

re: Onchange Event in Dropdown


Quote:

Originally Posted by waqasahmed996 View Post

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

  1. submit the form
  2. submit an AJAX request
Member
 
Join Date: Jun 2009
Posts: 37
#7: Jun 11 '09

re: Onchange Event in Dropdown


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.
Member
 
Join Date: Jun 2008
Posts: 97
#8: Jun 11 '09

re: Onchange Event in Dropdown


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.  
Member
 
Join Date: Jun 2009
Posts: 37
#9: Jun 11 '09

re: Onchange Event in Dropdown


I am using ASP pages and javascript, not asp.net or php coding.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,652
#10: Jun 11 '09

re: Onchange Event in Dropdown


that doesn't change the general idea, only the syntax.
Member
 
Join Date: Jun 2009
Posts: 37
#11: Jun 11 '09

re: Onchange Event in Dropdown


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
Member
 
Join Date: Jun 2008
Posts: 97
#12: Jun 11 '09

re: Onchange Event in Dropdown


Dormilich please can you tell me correct syntax. my above code is not executed correctly
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,652
#13: Jun 11 '09

re: Onchange Event in Dropdown


I know neither ASP nor ASP.NET
Member
 
Join Date: Jun 2008
Posts: 97
#14: Jun 11 '09

re: Onchange Event in Dropdown


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.  
Member
 
Join Date: Jun 2009
Posts: 37
#15: Jun 11 '09

re: Onchange Event in Dropdown


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.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,652
#16: Jun 11 '09

re: Onchange Event in Dropdown


Quote:

Originally Posted by waqasahmed996 View Post

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  $country = echo '<script type="text/JavaScript"> echo value; </script>'; 
  3.  ?> 
  4.  

echo is not a function and thus cannot be assigned to a variable.
Member
 
Join Date: Jun 2008
Posts: 97
#17: Jun 11 '09

re: Onchange Event in Dropdown


thanks chandhseke but i am asking from Dormilich
Member
 
Join Date: Jun 2009
Posts: 37
#18: Jun 11 '09

re: Onchange Event in Dropdown


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
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#19: Jun 11 '09

re: Onchange Event in Dropdown


Quote:

Originally Posted by chandhseke View Post

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

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.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,652
#20: Jun 11 '09

re: Onchange Event in Dropdown


Quote:

Originally Posted by chandhseke View Post

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

what do you mean by "text box"? a <textarea> or a <input> or a <div> element?
Member
 
Join Date: Jun 2008
Posts: 97
#21: Jun 11 '09

re: Onchange Event in Dropdown


you can write like
document.getElementById('txtbox').innerHTML = 'INDIA'

Dormilich i remove echo from my code even then it is not working
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,652
#22: Jun 11 '09

re: Onchange Event in Dropdown


Quote:

Originally Posted by waqasahmed996 View Post

Dormilich i remove echo from my code even then it is not working

to get output you have to use echo or one of the print() functions
Member
 
Join Date: Jun 2008
Posts: 97
#23: Jun 11 '09

re: Onchange Event in Dropdown


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.  
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,652
#24: Jun 11 '09

re: Onchange Event in Dropdown


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.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#25: Jun 11 '09

re: Onchange Event in Dropdown


Quote:

Originally Posted by waqasahmed996 View Post

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.  

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.
Member
 
Join Date: Jun 2009
Posts: 37
#26: Jun 26 '09

re: Onchange Event in Dropdown


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>
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#27: Jun 28 '09

re: Onchange Event in Dropdown


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.
Member
 
Join Date: Jun 2009
Posts: 37
#28: Jun 29 '09

re: Onchange Event in Dropdown


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.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#29: Jun 29 '09

re: Onchange Event in Dropdown


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.
Member
 
Join Date: Jun 2009
Posts: 37
#30: Jun 29 '09

re: Onchange Event in Dropdown


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..??
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#31: Jun 29 '09

re: Onchange Event in Dropdown


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;
Member
 
Join Date: Jun 2009
Posts: 37
#32: Jun 29 '09

re: Onchange Event in Dropdown


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...
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#33: Jun 29 '09

re: Onchange Event in Dropdown


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.
Member
 
Join Date: Jun 2009
Posts: 37
#34: Jun 29 '09

re: Onchange Event in Dropdown


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>
Member
 
Join Date: Jun 2009
Posts: 37
#35: Jun 29 '09

re: Onchange Event in Dropdown


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>
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#36: Jun 30 '09

re: Onchange Event in Dropdown


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.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#37: Jun 30 '09

re: Onchange Event in Dropdown


Quote:

Originally Posted by chandhseke View Post

As suggested i had made the below code changes in my form, when i tested my database it contains Blank value.

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.
Member
 
Join Date: Jun 2009
Posts: 37
#38: Jun 30 '09

re: Onchange Event in Dropdown


Hi folks,

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

Many thanks
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#39: Jul 1 '09

re: Onchange Event in Dropdown


No problem. Glad it's now working :)
Member
 
Join Date: Jun 2009
Posts: 37
#40: Jul 1 '09

re: Onchange Event in Dropdown


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
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#41: Jul 1 '09

re: Onchange Event in Dropdown


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.
Member
 
Join Date: Jun 2009
Posts: 37
#42: Jul 1 '09

re: Onchange Event in Dropdown


Ok Sorry for that.. Thank you
Reply