its my first php exercice and i am asked to make a form in a php file and show result in another file i chose the post method
then i have been asked to show a javascript alert if a field is empty
the fist part was fine, but im struggling to mix php js in the submit button for the second part. i tried changing order of code i put the php and script below the form but it still didnt work
file 1
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ex4</title>
</head>
<body>
<?php
function check_input(){
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST['nom']) || empty($_POST['prenom']) || empty($_POST['adresse']) || empty($_POST['code_postal'])){
echo "window.alert("un ou plusieurs champs ne sont pas remplis !");";
}
}
}
?>
<script>
function myFunction() {
<?php check_input(); ?>
}
</script>
<form method="post" action="ex4sep.php">
<fieldset style ="display: inline-block;">
<legend>Informations Etudiants</legend>
<table>
<tr>
<td style="width:50%;">nom</td>
<td><input type="text" name="nom"></td>
</tr>
<tr>
<td style="width:50%;">prenom</td>
<td><input type="text" name="prenom"></td>
</tr>
<tr>
<td style="width:50%;">adresse</td>
<td><input type="text" name="adresse"></td>
</tr>
<tr>
<td style="width:50%;">code postal</td>
<td><input type="text" name="code_postal"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" onclick="myFunction"></td>
<td></td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>
file 2 of results
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ex4sep</title>
</head>
<body>
<table border>
<tr>
<td>nom</td>
<td><?php echo $_POST['nom']; ?></td>
</tr>
<tr>
<td>prenom</td>
<td><?php echo $_POST['prenom']; ?></td>
</tr>
<tr>
<td>adresse</td>
<td><?php echo $_POST['adresse']; ?></td>
</tr>
<tr>
<td>code postal</td>
<td><?php echo $_POST['code_postal']; ?></td>
</tr>
</table>
</body>
</html>