Diff between 1b8def5718b9afa1af727e7cf12b781a5bd786af and 495b474c43bac4ec9b83d9e24b3c28db6918ad0d

Changed Files

File Additions Deletions Status
public/uusopiskelija.php +52 -19 modified

Full Patch

diff --git a/public/uusopiskelija.php b/public/uusopiskelija.php
index 031e552..3a79738 100644
--- a/public/uusopiskelija.php
+++ b/public/uusopiskelija.php
@@ -7,6 +7,19 @@
 		header('Location: '.$newURL);
 		die();
 	}
+	// Otetaan tietokannasta harjoittelupaikkojen tiedot jotta voi autofill tiedot sieltä
+	// Annetaan tiedot javascriptille :fear:
+	
+	// SQL komento: saadaan oppilaat
+	$sql = "SELECT * FROM harjoittelupaikat";
+	$result = $conn->query($sql); // Aja komento
+	// Jos tuloksia
+	$paikat = [];
+	if ($result->num_rows>0){
+		while($row = $result->fetch_assoc()){
+			$paikat[] = $row;
+		}
+	}
 ?>
 
 <!DOCTYPE html>
@@ -15,7 +28,37 @@
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>Lisää opiskelija</title>
-    <link rel="stylesheet" type="text/css" href="../css/style.css">
+	<link rel="stylesheet" type="text/css" href="../css/style.css">
+	<script>
+		/**
+		 * Class hpaikka
+		 * @author Wisdurm
+		 */
+		class hpaikka
+		{
+			constructor(ohjaaja, yhteystiedot) {
+				this.ohjaaja = ohjaaja;
+				this.yhteystiedot = yhteystiedot;
+			}
+		}
+		// Tee dictionary jossa paikkojen nimet vastaa niiden tietoja
+		var paikka_map = new Map();
+		<?php
+			foreach ($paikat as $paikka) {
+				echo "paikka_map.set('{$paikka['nimi']}', new hpaikka('{$paikka['ohjaaja']}', '{$paikka['yhteystiedot']}'));";
+			}
+		?>
+		console.log(paikka_map);
+
+		function autofill(){
+			// Paikka arvo
+			let paikka = document.getElementById("paikka").value;
+			if (paikka_map.has(paikka)) { // Tietokanta paikka löydetty
+				document.getElementById("ohjaaja").value = paikka_map.get(paikka).ohjaaja;
+				document.getElementById("yhteystiedot").value = paikka_map.get(paikka).yhteystiedot;
+			} 
+		}
+	</script>
 </head>
 <body>
 	<?php include "../src/header.php"; ?>
@@ -37,7 +80,14 @@
 		<fieldset>
 			<legend>Yrityksen tiedot</legend>
 			<label for="paikka">Paikka:</label>
-			<input type="text" id="paikka" name="paikka" placeholder="Kpedu">
+			<input type="text" id="paikka" name="paikka" placeholder="Kpedu" list="hpaikat" oninput="autofill()">
+			<datalist id="hpaikat">
+				<?php
+	 				foreach ($paikat as $paikka) {
+						echo "<option value='{$paikka['nimi']}'></option>";
+					}
+				?>
+			</datalist>
 			<br>
 			<label for="ohjaaja">Ohjaaja:</label>
 			<input type="text" id="ohjaaja" name="ohjaaja" placeholder="Jaakko Pekka">
@@ -50,23 +100,6 @@
 			<br>
 			<label for="lopetus">Lopetus:</label>
 			<input type="date" id="lopetus" name="lopetus" required>
-			 <div class="text-container">
-
-            <input type="text" list="programmingLanguages" placeholder="Yritys.." />
-            <datalist id="programmingLanguages">
-                <option value="Objective C">Objective C</option>
-                <option value="C++">C++</option>
-                <option value="C#">C#</option>
-                <option value="Cobol">Cobol</option>
-                <option value="Go">Go</option>
-                <option value="Java">Java</option>
-                <option value="JavaScript">JavaScript</option>
-                <option value="Python">Python</option>
-                <option value="PHP">PHP</option>
-                <option value="Pascal">Pascal</option>
-                <option value="Perl">Perl</option>
-                <option value="R">R</option>
-                <option value="Swift">Swift</option>
 			<br>
 		</fieldset>
 		<fieldset>