How can i validate this form before submitting it to php
eg: if i do not enter the name and clicks submitt, the item gets saved in the database
<!DOCTYPE html>
<html>
<head>
<title>Add Student</title>
</head>
<body bgcolor="#192C3D" text="white">
<center>
<h2>Enter Student Details</h2><br><br>
<form action="student_add.php" method="post">
Register no:
<input type="text" name="reg_no"><br><br>
Name:
<input type="text" name="name"><br><br>
Gender:
<input type="radio" name="gender" value="M">Male
<input type="radio" name="gender" value="F">Female
<br><br>Group:
<select name="group" size="1">
<option value="Computer Science" selected>Computer Science</option>
<option value="Biology">Biology</option>
<option value="Commerce">Commerce</option>
<option value="Humanities">Humanities</option>
</select><br><br>
<input type="submit" value="Save"> 
<input type="reset" value="Clear">
</form>
</center>
</body>
</html>
And this is
student_add.php
<html>
<head><title>Add Student</title></head>
<body bgcolor="#192C3D" text="white" alink="#75EA7E" vlink="#75EA7E" link="#75EA7E">
<a href="add_student.php"><h3>Add Student</h3></a>
<a href="Show_student.php"><h3>Show Students</h3></a>
<?php
$link = mysqli_connect("localhost", "root", "rootuser", "githin");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$reg_no = mysqli_real_escape_string($link, $_REQUEST['reg_no']);
$name = mysqli_real_escape_string($link, $_REQUEST['name']);
$gender = mysqli_real_escape_string($link, $_REQUEST['gender']);
$batch = mysqli_real_escape_string($link, $_REQUEST['group']);
// attempt insert query execution
$sql = "INSERT INTO student (reg_no, name, gender, batch) VALUES ('$reg_no', '$name', '$gender', '$batch')";
if(mysqli_query($link, $sql)){
echo '<script language="javascript">';
echo 'alert("Record Successfully Added")';
echo '</script>';
} else{
echo '<script language="javascript">';
echo 'alert("Record Could NOT be added Successfully" . mysqli_error($link))';
echo '</script>';
}
// close connection
mysqli_close($link);
?>
</body>
</html>
Anyone please help me to validate the above form The name, reg_no and gender fields are mandatory if we try to submit the form without entering the mandatory fields, a javascript alert() should be appeared asking to fill the mandatory fields
mysqliyou should be using parameterized queries andbind_paramto add user data to your query. DO NOT use manual escaping and string interpolation or concatenation to accomplish this because you will create severe SQL injection bugs. Accidentally unescaped data is a serious risk. Using bound parameters is less verbose and easier to review to check you’re doing it properly.cURLcommand to your site and bypass your html validation, so you need to take advantage of functions liketrim(),empty(),isset(),preg_match(),filter_var(), etc. server-side. Client-side validation is more of a UX feature.