title: validate form before submitting (more complicated than checking for empty fields)
body: <p>i have a form containing inputs for times (specifically, an opening and closing time). when the submit button is pressed, it goes to a php page where these inputs are added to a database. i want to check a few things before allowing the form to submit. for example, i want to make sure that the start time is earlier than (less than) the end time. here's the form:</p>
  
  <pre><code>            Opens:
              &lt;select name="starthour1"&gt;
                  &lt;option value="00"&gt;12&lt;/option&gt;
                  &lt;option value="01"&gt;1&lt;/option&gt;
                  &lt;option value="02"&gt;2&lt;/option&gt;
                  &lt;option value="03"&gt;3&lt;/option&gt;
                  &lt;option value="04"&gt;4&lt;/option&gt;
                  &lt;option value="05"&gt;5&lt;/option&gt;
                  &lt;option value="06"&gt;6&lt;/option&gt;
                  &lt;option value="07"&gt;7&lt;/option&gt;
                  &lt;option value="08"&gt;8&lt;/option&gt;
                  &lt;option value="09"&gt;9&lt;/option&gt;
                  &lt;option value="10"&gt;10&lt;/option&gt;
                  &lt;option value="11"&gt;11&lt;/option&gt;
              &lt;/select&gt; : 
              &lt;select name="startminute1"&gt;
                  &lt;option value="00"&gt;00&lt;/option&gt;
                  &lt;option value="15"&gt;15&lt;/option&gt;
                  &lt;option value="30"&gt;30&lt;/option&gt;
                  &lt;option value="45"&gt;45&lt;/option&gt;
                  &lt;option value="59"&gt;59&lt;/option&gt;
              &lt;/select&gt; 
              &lt;select name="startwhen1"&gt;
                  &lt;option value="am"&gt;am&lt;/option&gt;
                  &lt;option value="pm"&gt;pm&lt;/option&gt;
              &lt;/select&gt;
  
              Closes:
              &lt;select name="endhour1"&gt;
                  &lt;option value="00"&gt;12&lt;/option&gt;
                  &lt;option value="01"&gt;1&lt;/option&gt;
                  &lt;option value="02"&gt;2&lt;/option&gt;
                  &lt;option value="03"&gt;3&lt;/option&gt;
                  &lt;option value="04"&gt;4&lt;/option&gt;
                  &lt;option value="05"&gt;5&lt;/option&gt;
                  &lt;option value="06"&gt;6&lt;/option&gt;
                  &lt;option value="07"&gt;7&lt;/option&gt;
                  &lt;option value="08"&gt;8&lt;/option&gt;
                  &lt;option value="09"&gt;9&lt;/option&gt;
                  &lt;option value="10"&gt;10&lt;/option&gt;
                  &lt;option value="11"&gt;11&lt;/option&gt;
              &lt;/select&gt; : 
              &lt;select name="endminute1"&gt;
                  &lt;option value="00"&gt;00&lt;/option&gt;
                  &lt;option value="15"&gt;15&lt;/option&gt;
                  &lt;option value="30"&gt;30&lt;/option&gt;
                  &lt;option value="45"&gt;45&lt;/option&gt;
                  &lt;option value="59"&gt;59&lt;/option&gt;
              &lt;/select&gt; 
              &lt;select name="endwhen1"&gt;
                  &lt;option value="am"&gt;am&lt;/option&gt;
                  &lt;option value="pm"&gt;pm&lt;/option&gt;
              &lt;/select&gt;
  </code></pre>
  
  <p>i found javascript code for how to check individual form elements, but i can't figure out how i could combine multiple without submitting. in my php page, i have the following code:</p>
  
  <pre><code>$starthour1=$_POST['starthour1'];
  $startminute1=$_POST['startminute1'];
  $startwhen1=$_POST['startwhen1'];
  $endhour1=$_POST['endhour1'];
  $endminute1=$_POST['endminute1'];
  $endwhen1=$_POST['endwhen1'];
  
  $start_time1=$end_time1="";
  
      if($startwhen1=="pm"){
          $starthour1=$starthour1+12;
      }
      if($endwhen1=="pm"){
          $endhour1=$endhour1+12;
      }
      $start_time1=$starthour1.":".$startminute1.":00";
      $end_time1=$endhour1.":".$endminute1.":00";
      if($end_time1=="00:00:00"){
          $end_time1="23:59:00";
      }
  
  echo "&lt;br&gt;start time is ".$start_time1;
  echo "&lt;br&gt;end time is ".$end_time1;
  
  $startnum=str_replace(":", "", $start_time1);
  $endnum=str_replace(":", "", $end_time1);
  if($endnum&lt;$startnum){
      echo "&lt;br&gt;start time can't be later than end time!";
  }
  else{
     //add to database
  }
  </code></pre>
  
  <p>however, checking this stuff after submitting doesn't make sense. i suppose i could redirect back to the initial page if the error was found, but that doesn't seem efficient.</p>
  
  <p>also, i was thinking maybe i could have some php on the page with the form that checks for validity. if it validates, it then posts to the php page that inserts stuff into the database. does this make sense and is it possible?</p>
  
  <p>is there a better solution? i imagine there is something i could do with javascript but i haven't been able to figure it out.</p>
  
  <p>additionally i'd like to inform the user of invalid inputs with text that appears next to the input box. i should be able to figure this out once the rest is working though.</p>
  
  <p>thanks.</p>