Drop down menu failed to validate

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

    Drop down menu failed to validate

    Hi..guys.I have a static display drop down menu,however it doesn't go through the validation function when option is not selected.Thanks for your generious help...

    Code:
    <script language="JavaScript" type="text/JavaScript">
    function formSubmit(ticket)
    {
    	var returnStatus = 1;
    
        if (ticket.rNumber.selectedIndex == 0) { 
    		alert("Please select number of ticket!");
    		returnStatus = 0;
    	};
           
    	if (returnStatus) { 
    		window.location='home.php'
    	}
    }
    </script>
    Code:
    <select name="rNumber" id="select4">
                          <option selected>Select</option>
                          <option>01</option>
                          <option>02</option>
                          <option>03</option>
                          <option>04</option>
                          <option>05</option>
                          <option>06</option>
    </select>
    Code:
    <form action="" method="post" 
    name="movieList" id="movieList">
    .......
    .......
    .......
    <input name="request" type="submit" id="request" value="Submit Request"    onclick="formSubmit(document.movieList)">
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Use return false after the alert on line 8.

    Instead of 0 and 1, consider using a boolean (true/false).

    If you just change the location, the form value will not be sent to the next page. Set an action and let the form submit.

    Comment

    • vinceboy
      New Member
      • Sep 2007
      • 22

      #3
      Actually the <form>tag was placed wrongly.However ,when mixed with another dynamic radio button written in PHP,the validation of radio button become failed.

      Code:
      <script language="JavaScript" type="text/JavaScript">
      function formSubmit(ticket)
      {
      	var returnStatus = 1;
      
          if (ticket.rNumber.selectedIndex == 0){ 
      		alert("Please select number of ticket!");
      		returnStatus = 0;
      	};
         //if( !(movieList.time[php echo $rows['name'];?>].checked)){
        	    //alert("Please choose screening time!");
        	    //returnStatus = 0;
      	 //};   
      	if (returnStatus) { 
      		window.location='home.php';
      	};
      }
      </script>
      [PHP]

      <?
      if (isset($_SESSIO N['gmemberid'])) {

      $tbl_name = "movie";
      $result = mysql_query(spr intf('SELECT name,classifica tion,screeningT ime FROM %s
      LIMIT 7', $tbl_name)) or die('Cannot execute query.');


      //$numrow = mysql_num_rows( $result);


      while ($rows = mysql_fetch_ass oc($result)) {
      echo '<table width="100%" border="0"><tr> <td height="68">
      <table width="100%" height="47" border="0">
      ---------------------------------------------------------------------------------------------------------<br>';

      echo '<strong>' . $rows['name'] . ' (' . $rows['classification '] . ')
      <br></strong>';
      foreach (explode(',', $rows['screeningTime']) as $time) { ?>
      <label>
      <input type="radio" name="time[<?php echo $rows['name']; ?>]"
      value="<?php echo $time; ?>">
      <?php echo $time; ?>&nbsp;&nbsp;& nbsp;
      </label>
      <?php } ?>
      <?
      }


      }
      ?>

      [/PHP]

      Code:
      <input name="request" type="submit" id="request" value="Submit Request"  onclick="formSubmit(document.movieList)">

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        To validate a form, use the onsubmit event:
        [HTML]<form name="..." action="..." onsubmit="retur n formSubmit(this );">[/HTML] In formSubmit(), returnValue should be set to true or false. If the validation passes, return true. If it fails, return false.

        Comment

        Working...