I have a system to apply permit and when they select the permit they like to apply, they will submit the form and the system will check if they have any previous applied permit that is same as the pre requisites.

In gerneral: If they do not meet the pre requisites, system should pop up a window and tell them 'Sorry, you do not have the pre-required permits'.

Else system will check if they have completed a medical questionnaire before. If they do have an existing medical questionnaire, system will store the application details and go to the apply_successful.php page. Else System will take them to medical_questions.php to do the questionnaire.

I tried doing the code out but was stuck at some point of it.

I Tried this so far:

Code:
<?php
session_start();
include'Connections/database.php';
$conn = dbConnect ();
    if (! $conn)
    die("Couldn't connect to MySQL");

    $user = $_SESSION['eid'];

    $query="select PTYPE from permit where EID = $user";
    $result = mysql_query($query, $conn);
    $row = mysql_fetch_assoc($result);
    $permit_arr=$row['PTYPE'];

    foreach($permit_arr as $permit)
    {
              $query="SELECT t.PREREQ1, t.PREREQ2
                      , (CASE WHEN (t.PREREQ1 IS NOT   NULL) AND (p1.PTYPE IS NULL) THEN 1 ELSE 0 END) AS missing1
                      , (CASE WHEN (t.PREREQ2 IS NOT NULL) AND (p2.PTYPE IS NULL) THEN 1 ELSE 0 END) AS missing2 
                      FROM type AS t 
                      LEFT JOIN permit AS p1 ON (t.PREREQ1=p1.ptype) AND ( p1.EID = $user ) 
                      LEFT JOIN permit AS p2 ON (t.PREREQ2=p2.ptype) AND ( p2.EID = $user ) 
                      WHERE ( t.PTYPE = $permit )";
             $result = mysql_query($query, $conn);
             $row = mysql_fetch_assoc($result);
             $missing1= $row['missing1'];
             $missing2= $row['missing2'];
    }

    if($missing1 =='1' or $missing2 =='1')
    {

        //I am stuck here...I would like to putan alert() here to notify user 'Sorry,you do not have the pre-required permits...and also stay on the same page which is apply2.php
    }
    else
    {

          $query = "select MED  from emp where EID = '$user'";
          $result = mysql_query($query, $conn);
          $row = mysql_fetch_assoc($result);

              $med = $row['MED'];   
          $user =$_SESSION['eid'];
          $cat_arr=$_POST['cat'];


              foreach ($cat_arr as $cat) 
          {
         $cat = mysql_real_escape_string($cat);

                   if ($med == 'yes')
               {
                         $query = "INSERT INTO permit (EID, PTYPE) VALUES ('{$user}', '{$cat}')";
               }
                   else
               {
                       //do nothing
                }

                    mysql_query($query);
          }

dbDisconnect($conn);

if ($med == 'no')
$nextPage = "medical_question.php";
else
$nextPage = "s_apply_success.php";

die(header("Location:{$nextPage}"));
    }
   ?>
I am stuck at the part where I made a comment that I am stuck...which is roughly line 28.

I wonder if anyone can guide me on how should I continue?

Thanks so much...help is greatly appreciated.