SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to prevent auto redirect of drop down menu?

    Hi..guys!I have a drop down menu with javascript null validation.However,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...

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    remove the onchange attribute from the select tag - will prevent execution of the GoToNextPage function.

  3. #3
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't remove it because need to display the number of ticket in next page.
    PHP Code:
    <?= $_GET['selected_no_ticket']; ?>
    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,validate function can't work at all and the selected value are not displayed in next page.

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The name of the select element is rNumber, so if you submit the form you should be able to get that value from the next page using $_POST['rNumber']


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •