Onchange Event in Dropdown

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chandhseke
    New Member
    • Jun 2009
    • 92

    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,
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you could use window.confirm( )?

    Comment

    • chandhseke
      New Member
      • Jun 2009
      • 92

      #3
      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?

      Comment

      • chandhseke
        New Member
        • Jun 2009
        • 92

        #4
        Code:
        <html>
        <head>
        <script language="javascript">
        function ConSub()
          {
           var result;
           var w = document.frm.Country.selectedIndex;
           var selected_text = document.frm.Country.options[w].text;
             if (selected_text == "South Africa")
                'result=window.confirm("Do you have a Emp certificate?");
                         if (result== true)
                      // i want to store some value sayYES in a variable and call this variable in a text box//
        else 
        return false;
        }
        <form name="frm">
        <select name="Country" onchange="return ConSub()">
        <option value="India">India
        <option value="South Africa">South Africa
        </select>
        </form>
        </body>
        </html>
        Please edit/comment on how can i track that user has clicked OK or cancel.i want store this in a variable.
        Last edited by Dormilich; Jun 11 '09, 12:10 PM. Reason: Please use [code] tags when posting code

        Comment

        • waqasahmed996
          New Member
          • Jun 2008
          • 160

          #5
          this code may be helpfull for you
          Code:
          <script language="javascript">
          function country (value)
          {
          if(window.confirm("Are You "+value+" citizan"))
               {
          	 country = value;
          	  return true;
               }
          else
                return false;
          }
          </script>
          
          <select name="country" id="country" onchange="return country(this.value)">
          <option value="0">Select Country</option>
          <option value="India">India</option>
          <option value="United States">United States</option>
          <option value="South Africa">South Africa</option>
          </select>
          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

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Originally posted by waqasahmed996
            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

            Comment

            • chandhseke
              New Member
              • Jun 2009
              • 92

              #7
              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.

              Comment

              • waqasahmed996
                New Member
                • Jun 2008
                • 160

                #8
                any other way like
                Code:
                 if(window.confirm("Are You "+value+" citizan"))
                      {
                    <?php
                    $country = echo '<script type="text/JavaScript"> echo value; </script>'; 
                    ?>      
                
                       return true;
                      }

                Comment

                • chandhseke
                  New Member
                  • Jun 2009
                  • 92

                  #9
                  I am using ASP pages and javascript, not asp.net or php coding.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    that doesn't change the general idea, only the syntax.

                    Comment

                    • chandhseke
                      New Member
                      • Jun 2009
                      • 92

                      #11
                      I agree... According my script,
                      var w = document.frm.Co untry.selectedI ndex;
                      var selected_text = document.frm.Co untry.options[w].text;
                      Variable "selected_t ext" 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

                      Comment

                      • waqasahmed996
                        New Member
                        • Jun 2008
                        • 160

                        #12
                        Dormilich please can you tell me correct syntax. my above code is not executed correctly

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          I know neither ASP nor ASP.NET

                          Comment

                          • waqasahmed996
                            New Member
                            • Jun 2008
                            • 160

                            #14
                            no i am asking about php and javascript. what is mistake in my following code. where variable 'value' is javascript variable
                            Code:
                            <?php
                             $country = echo '<script type="text/JavaScript"> echo value; </script>'; 
                             ?>

                            Comment

                            • chandhseke
                              New Member
                              • Jun 2009
                              • 92

                              #15
                              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.

                              Comment

                              Working...