

<!DOCTYPE HTML>  
<html>
<head>
     <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://getbootstrap.com/docs/5.3/assets/css/docs.css" rel="stylesheet">
    <title>Contact us</title>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
  
  <style>
.error {color: #FF0000;}
body {
  background-color: aqua;
}
</style>
  
  </head>


<body class="p-3 m-0 border-0 bd-example m-0 border-0">  

        



<?php
// define variables and set to empty values
//to1 variable is used name the variable of the school

$titleErr = $postErr =$surnameErr = $cellnumErr = $whatsappErr = $emailErr =  $hearErr =  $to1Err =   $pass1Err = $pass2Err = $namesErr = "";

$title = $post =  $surname = $cellnum = $whatsapp = $email =  $hear =  $to1  = $pass1 = $pass2 = $names = "";

//other variables

    $counter = 0 ;

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  
  //codes for title
  if (empty($_POST["title"])) {
    $titleErr = "Title is required";
  } else {
    $title = test_input($_POST["title"]);
    $counter++;
  }
   //codes for post
  if (empty($_POST["post"])) {
    $postErr = "Post is required";
  } else {
    $post = test_input($_POST["post"]);
    $counter++;
  }
  
  //codes for surname
  if (empty($_POST["surname"])) {
    $surnameErr = "Surname is required";
  } else {
    $surname = test_input($_POST["surname"]);
    $surname = wording($_POST["surname"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z-' ]*$/",$surname)) {
      $surnameErr = "Only letters and white space allowed";
    } else {
        $counter++;
    }
  }
  
  
  
  //codes for cellnum
  if (empty($_POST["cellnum"])) {
    $cellnumErr = "Cell number is required";
  } else {
    $cellnum = test_input($_POST["cellnum"]);
    $celllen = strlen($cellnum);
    $cell = "9999999999";
    $cell = strlen($cell);
    
    if ($celllen === $cell){
        $counter++;
    }else {
        $cellnumErr = "Cell number must be 10 digits";
    }
    
  }
  
  //codes for whatsapp number
  if (empty($_POST["whatsapp"])) {
    $whatsappErr = "Whatsapp number is required";
  } else {
    $whatsapp = test_input($_POST["whatsapp"]);
    $celllen = strlen($whatsapp);
    $cell = "9999999999";
    $cell = strlen($cell);
    
    if ($celllen === $cell){
        $counter++;
    }else {
        $whatsappErr = "Whatsapp number must be 10 digits";
    }
    
  }
  
  //codes for e-mail address
  
   if (empty($_POST["email"])) {
    $emailErr = "Email is optional";
  } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format";
    }
  }
 
    
  
  //codes for password 1
  if (empty($_POST["pass1"])) {
    $pass1Err = "Password is required";
  } else {
    $pass1 = test_input($_POST["pass1"]);
    
  }
  
  //codes for password 2
  if (empty($_POST["pass2"])) {
    $pass2Err = "Confirm password is required";
  } else {
    $pass2 = test_input($_POST["pass2"]);
    
  }
  //comparing password 1 with password 2 
  if ($pass1 === $pass2 ){
      $counter++;
  } else{
       $pass1Err = "Mismatch password ";
       $pass1Err = "Mismatch password ";
  }
  
  //codes for hear about us
  if (empty($_POST["hear"])) {
    $hearErr = "Please select one option.";
  } else {
    $hear = test_input($_POST["hear"]);
    $counter++;
  }
  
 
  
   //codes for transfer to1
  if (empty($_POST["to1"])) {
    $to1Err = "School name is required";
  } else {
    $to1 = test_input($_POST["to1"]);
    $counter++;
  }
  
 
  
  
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
function wording($data) {
  $data = strtolower($data);
  $data = ucwords($data);
  return $data;
}

?>

<u><center><h2>Your contact info</h2></center></u>
<p><span class="error">* required field</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
    
    
    <div class="form-floating">
  <select class="form-select" id="title" name = "title" aria-label="title"  value="<?php echo $title;?>" required>
    <option selected></option>
    <option value="Dr">Dr</option>
    <option value="Mr">Mr</option>
    <option value="Mrs">Mrs</option>
    <option value="Miss">Miss</option>
    <option value="Ms">Ms</option>
    
  </select>
  <label for="floatingSelect">Title</label>
</div> <br />

    <div class="form-floating">
  <select class="form-select" id="post" name = "post" aria-label="title"  value="<?php echo $post;?>" required>
    <option selected></option>
    <option value="Circuit manager">Circuit manager</option>
    <option value="Principal">Principal</option>
     <option value="SGB Chairperson">SGB Chairperson</option>
    <option value="SGB Deputy Chairperson">SGB Deputy Chairperson</option>
    <option value="Parent">Parent</option>
    <option value="Teacher component">Teacher Component</option>
    <option value="Non-teaching component">Non-teaching component</option>
    <option value="Learner">Learner</option>
  
  </select>
  <label for="floatingSelect">Post Occupied (SGB)</label>
</div> <br />
  
  <div class="form-floating mb-3">
  <input type="text" class="form-control" name="surname" id="surname" placeholder="Surname" value="<?php echo $surname;?>" required autofocus minlength="3" maxlength="30">
  <span class="error">* <?php echo $surnameErr;?></span>
  <br>
  <label for="surname">Surname</label>
</div>

   
<div class="form-floating mb-3">
  <input type="text" class="form-control" name="cellnum" id="cellnum" placeholder="Cell number" value="<?php echo $cellnum;?>" required minlength="10" maxlength="10" >
  <span class="error">* <?php echo $cellnumErr;?></span>
  
  <label for="cellnum">Cell number</label>
</div>


<div class="form-floating mb-3">
  <input type="text" class="form-control" name="whatsapp" id="whatsapp" placeholder="Whatsapp number" value="<?php echo $whatsapp;?>" required minlength="10" maxlength="10" >
  <span class="error">* <?php echo $whatsappErr;?></span>
  
  <label for="whatsapp">Whatsapp number</label>
</div>

    <div class="form-floating mb-3">
  <input type="text" class="form-control" name="email" id="email" placeholder="E-mail address (Optional)" value="<?php echo $email;?>"autofocus minlength="10" maxlength="50">
  <span class="error">* <?php echo $emailErr;?></span>
  <br>
  <label for="surname">E-mail address (Optional)</label>
</div>

   
         <label for="exampleDataList" class="form-label" style="color:red;">School currently serving :  </label>
<input class="form-control" list="towns" id="exampleDataList" name= "to1" placeholder="Type and select your school name." required >
<datalist id="towns">
  <option value="">
  
  
  <?php
$servername = "localhost";
$username = "commemay_sono";
$password = "Masiu@5$5$5$";
$dbname = "commemay_csc";

        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
              die("Connection failed: " . $conn->connect_error);
        }

        $sql = "SELECT school FROM schools ORDER BY school ASC";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
         // output data of each row
         while($row = $result->fetch_assoc()) {
       
       
       $school = $row["school"];
        
      
       echo '<option value="'.$school.'">';
       
       
         }
            } 
        $conn->close();
?> 
  
    </datalist>
    
    <br />
   
 <div class="form-floating">
  <select class="form-select" id="hear" name = "hear" aria-label="hear"  value="<?php echo $hear;?>" required>
    <option selected></option>
    <option value="Word of mouth">Word of mouth</option>
    <option value="Facebook">Facebook</option>
    <option value="Facebook group">Facebook group</option>
    <option value="Whatsapp group">Whatsapp group</option>
    <option value="Whatsapp">Whatsapp</option>
    <option value="School">School</option>
    
    <option value="Other">Other</option>
    
  </select>
  <label for="floatingSelect">How did you hear about us</label>
</div> <br />
       
    <div class="d-grid gap-2 col-6 mx-auto">
  <button type="submit" class="btn btn-primary" type="button">Submit</button>
  
</div>
 
  
    </form>

<?php

 
       

        
        
        if ($counter === 8 ){
            
         
        
         //getting todays date
         $date = date("Y/m/d");
         
        
        //connecting to the database
        
        $servername = "localhost";
        $username = "commemay_sono";
        $password = "Masiu@5$5$5$";
        $dbname = "commemay_csc";

        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
              die("Connection failed: " . $conn->connect_error);
        }
        
        //checking if the selected school appear in the database
        $exist = "No";
        $sql = "SELECT school FROM schools WHERE school = '$to1' ";
        $result = $conn->query($sql);
        
        
         if ($result->num_rows > 0) {
         // output data of each row
         while($row = $result->fetch_assoc()) {
       
       $exist = "Yes";
            }
         }
         
         if ($exist != "Yes"){
         
         echo ("<script LANGUAGE='JavaScript'>
    window.alert('The selected school does not appear in our database. Please contact web developer @ 3420643@myuwc.ac.za so that we can include ur school.Thank you.');
    window.location.href='/index.php';
    </script>");
        
         }
        
        
        
        
        
        
        // prepare and bind
        $stmt = $conn->prepare("INSERT INTO contacts (title,post, surname , cellnum, whatsapp, email,school,hear,date) VALUES (?,?,?,?,?,?,?,?,?)");
        
        $stmt->bind_param("sssssssss", $title, $post, $surname, $cellnum,$whatsapp,$email,$to1,$hear,$date);
        
        $stmt->execute();

        $conn->close();
        
        
        //sending our welcome message
        
        
        //credentials used are the ones for LETZ CROSS
        /*
        $apiKey = '432447af-a456-49a3-955b-c43053432837';
        $apiSecret = '3bfa90bf-25c7-4945-9317-16eb3b22f770';
        $accountApiCredentials = $apiKey . ':' . $apiSecret;

        $base64Credentials = base64_encode($accountApiCredentials);
        $authHeader = 'Authorization: Basic ' . $base64Credentials;
        
         //creating a message
        
        
        $msg = 'Good day '.$title.' '.$surname.'. Thank you 4 signing up at mysgb.co.za . ur password : '.$pass.' .';
        
        
        
        //Sending the message
        
         $sendData = '{ "messages" : [ { "content" : "'.$msg.'", "destination" : ">>'.$cellnum.'<<" } ] }';

$options = array(
    'http' => array(
        'header' => array("Content-Type: application/json", $authHeader),
        'method' => 'POST',
        'content' => $sendData,
        'ignore_errors' => true
    )
);


$sendResult = file_get_contents('https://rest.smsportal.com/bulkmessages', false, stream_context_create($options));

$status_line = $http_response_header[0];
preg_match('{HTTP\/\S*\s(\d{3})}', $status_line, $match);
$status = $match[1];

*/
        
        
        // Display the alert box 
        
        echo ("<script LANGUAGE='JavaScript'>
    window.alert('You have successfully submitted your info. We will contact you soon.Thank you.');
    
    
    
    window.location.href='/index.php';
    </script>");
        
        
            
        }


?>

</body>
</html>
