How to prevent auto redirect of drop down menu?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vinceboy
    New Member
    • Sep 2007
    • 22

    How to prevent auto redirect of drop down menu?

    Hi..guys!I have a drop down menu with javascript null validation.Howe ver,instead of clicking submit button,it will immediately auto redirect after option was selected.Is it possible to prevent this kind of fast respond?

    Code:
    <select name="rNumber" title="number of ticket"
    					 onChange="GoToNextPage(this.value)">
                          <option selected>Select</option>
                          <option>01</option>
                          <option>02</option>
                          <option>03</option>
                          <option>04</option>
                          <option>05</option>
                          <option>06</option>
     </select>
    Code:
    <script language="JavaScript" type="text/JavaScript">
        function validate(which) {
            var selects = which.getElementsByTagName('select');
            var radios = which.getElementsByTagName('input');
            for(sel = 0; sel < selects.length; sel++) {
                if(selects[sel].options[selects[sel].selectedIndex].text == 'Select') {
                    alert('Please select number of ticket!');
                    return false;
                }
            }
    
            var rselCount = 0;
            for(radio = 0; radio < radios.length; radio++) {
                if(radios[radio].checked) {
                  rselCount++;
                }
            }
    
            if(rselCount == 0) {
               alert('Please select screening time!');
               return false;
            }
            return true;
        }
        
       function GoToNextPage(value){
            if(value != ""){
           document.location = 'reservation3.php?selected_no_ticket=' + value;
      }
    }
    Code:
    <form action="reservation3.php" method="post" name="movieList" 
    onSubmit="return validate(this);">
    And also when onchange mixed with onsubmit,the validation can't execute
    alert('Please select screening time!'); when radio button was not selected.What went wrong in the script?Thanks.. .
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    GoToNextPage() is sending it to the next page, so just get rid of the onchange.

    Comment

    • vinceboy
      New Member
      • Sep 2007
      • 22

      #3
      I can't remove it because need to display the number of ticket in next page.
      [PHP]<?= $_GET['selected_no_ti cket']; ?>[/PHP]

      What I intend to do is capturing the selected value and display it in next page.In the form I have radio button and drop down menu which apply onclick and onchange attribute respectively in a single function.

      Code:
       function GoToNextPage(value){
              if(value != ""){
             document.location = 'reservation3.php?selected_no_ticket=' 
             +value+'selected_time=' + value;
        }
      }

      Code:
      <form action="reservation3.php" method="post" name="movieList" 
      		  onSubmit="return validate(this);">
      
      <select name="rNumber" title="number of ticket"onChange="GoToNextPage_ticket(this.value)">
      
      <input type="radio"  name="time[<?php echo $rows['name']; ?>]"
       title ="screening time" value="<?php echo $time; ?>"
      onClick ="GoToNextPage(this.value)">
      However,by combining them into one GoToNextPage function,valida te function can't work at all and the selected value are not displayed in next page.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Don't use onclick and onchange to pass form values. Let the submit take care of that.

        When the form is submitted, the validation function is run. If it validates, return true so the form submits and the values are passed to the next page. If it doesn't validate, return false so the form submission is stopped.

        Comment

        • vinceboy
          New Member
          • Sep 2007
          • 22

          #5
          You meant that changed onchange and onclick to onsubmit?

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by vinceboy
            You meant that changed onchange and onclick to onsubmit?
            No, when onsubmit returns true, the values will automatically be passed to the action page. Perhaps, you're accessing the values in the wrong way. You're using POST and in your PHP, you're trying to access with $_GET.

            Comment

            • vinceboy
              New Member
              • Sep 2007
              • 22

              #7
              I have changed accessing method to POST,however how can I display both selected value in next page without using GoToNextPage function,onchan ge and onclick?
              $_POST[' ?']

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by vinceboy
                I have changed accessing method to POST,however how can I display both selected value in next page without using GoToNextPage function,onchan ge and onclick?
                $_POST[' ?']
                Yes, you would use $_POST for post-ed form values, but now we're moving into PHP territory.

                Comment

                • vinceboy
                  New Member
                  • Sep 2007
                  • 22

                  #9
                  Can you help me to work out?Thanks for generious help....

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Originally posted by vinceboy
                    Can you help me to work out?Thanks for generious help....
                    With the PHP? I direct you to the capable hands of the experts in the PHP forum.Have you solved your original problem?

                    Comment

                    • vinceboy
                      New Member
                      • Sep 2007
                      • 22

                      #11
                      Solved already,Thanks a lot.Now validation control well but only thing can't display value in next page.

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Originally posted by vinceboy
                        Solved already,Thanks a lot.Now validation control well but only thing can't display value in next page.
                        Since it's not Javascript-related, just start a new thread in the PHP forum (if you haven't already done so).

                        Edit: I see that you have. Well, good luck with the rest of your project.

                        Comment

                        Working...