By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,148 Members | 759 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,148 IT Pros & Developers. It's quick & easy.

How to prevent auto redirect of drop down menu?

P: 22
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?

Expand|Select|Wrap|Line Numbers
  1. <select name="rNumber" title="number of ticket"
  2.                      onChange="GoToNextPage(this.value)">
  3.                       <option selected>Select</option>
  4.                       <option>01</option>
  5.                       <option>02</option>
  6.                       <option>03</option>
  7.                       <option>04</option>
  8.                       <option>05</option>
  9.                       <option>06</option>
  10.  </select>
  11.  
Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript" type="text/JavaScript">
  2.     function validate(which) {
  3.         var selects = which.getElementsByTagName('select');
  4.         var radios = which.getElementsByTagName('input');
  5.         for(sel = 0; sel < selects.length; sel++) {
  6.             if(selects[sel].options[selects[sel].selectedIndex].text == 'Select') {
  7.                 alert('Please select number of ticket!');
  8.                 return false;
  9.             }
  10.         }
  11.  
  12.         var rselCount = 0;
  13.         for(radio = 0; radio < radios.length; radio++) {
  14.             if(radios[radio].checked) {
  15.               rselCount++;
  16.             }
  17.         }
  18.  
  19.         if(rselCount == 0) {
  20.            alert('Please select screening time!');
  21.            return false;
  22.         }
  23.         return true;
  24.     }
  25.  
  26.    function GoToNextPage(value){
  27.         if(value != ""){
  28.        document.location = 'reservation3.php?selected_no_ticket=' + value;
  29.   }
  30. }
  31.  
Expand|Select|Wrap|Line Numbers
  1. <form action="reservation3.php" method="post" name="movieList" 
  2. onSubmit="return validate(this);">
  3.  
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...
Sep 25 '07 #1
Share this Question
Share on Google+
11 Replies


acoder
Expert Mod 15k+
P: 16,027
GoToNextPage() is sending it to the next page, so just get rid of the onchange.
Sep 25 '07 #2

P: 22
I can't remove it because need to display the number of ticket in next page.
[PHP]<?= $_GET['selected_no_ticket']; ?>[/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.

Expand|Select|Wrap|Line Numbers
  1.  function GoToNextPage(value){
  2.         if(value != ""){
  3.        document.location = 'reservation3.php?selected_no_ticket=' 
  4.        +value+'selected_time=' + value;
  5.   }
  6. }
  7.  

Expand|Select|Wrap|Line Numbers
  1. <form action="reservation3.php" method="post" name="movieList" 
  2.           onSubmit="return validate(this);">
  3.  
  4. <select name="rNumber" title="number of ticket"onChange="GoToNextPage_ticket(this.value)">
  5.  
  6. <input type="radio"  name="time[<?php echo $rows['name']; ?>]"
  7.  title ="screening time" value="<?php echo $time; ?>"
  8. onClick ="GoToNextPage(this.value)">
  9.  
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.
Sep 25 '07 #3

acoder
Expert Mod 15k+
P: 16,027
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.
Sep 25 '07 #4

P: 22
You meant that changed onchange and onclick to onsubmit?
Sep 25 '07 #5

acoder
Expert Mod 15k+
P: 16,027
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.
Sep 25 '07 #6

P: 22
I have changed accessing method to POST,however how can I display both selected value in next page without using GoToNextPage function,onchange and onclick?
$_POST[' ?']
Sep 25 '07 #7

acoder
Expert Mod 15k+
P: 16,027
I have changed accessing method to POST,however how can I display both selected value in next page without using GoToNextPage function,onchange and onclick?
$_POST[' ?']
Yes, you would use $_POST for post-ed form values, but now we're moving into PHP territory.
Sep 25 '07 #8

P: 22
Can you help me to work out?Thanks for generious help....
Sep 25 '07 #9

acoder
Expert Mod 15k+
P: 16,027
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?
Sep 25 '07 #10

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

acoder
Expert Mod 15k+
P: 16,027
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.
Sep 25 '07 #12

Post your reply

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