As the title states I am trying to do some basic html and php programs. I am trying to get information from the user/student and then print it to a text file. Where I am getting the error is after I enter the information on the html page it gives me the echo messages of first name wrong, last name wrong, id wrong, and incorrect gpa format. So something is wrong in regards to the information being saved properly from the html to php file I am just not sure what. The file names are Studentinfo.html and Studentinfo.php. . I haven't worked much with php so it could be a simple fix I am just unsure of what the issue is. Below is the html code.
<!DOCTYPE html>
<html>
<head><titile>Student information</title></head>
<body>
<table><form action="Studentinfo.php" method="POST">
<tr><td>First name</td><td><input type="text" id="fn"></td></tr>
<tr><td>Last name</td><td><input type="text" id="ln"></td></tr>
<tr><td>Student Id</td><td><input type="text" id="si"></td></tr>
<tr><td>Current GPA</td><td><input type="number" step="0.1" id="gpa"> </td></tr>
<tr><td><input type="submit" value="Submit" ></td><td><input type="reset" value="Cancel"></td></tr>
</form>
</table>
</body>
</html>
Below is the php code:
<?php
$firstname=$lastname=$stdid=$gps="";
$file="Studentinfo.txt";
$file_open=fopen($file,'w+');
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$firstname=$_POST["fn"];
$lastname=$_POST["ln"];
$stdid=$_POST["si"];
$gpa=$_POST["gpa"];
if((!preg_match("/^[a-zA-Z ]*$/",$firstname)) || (empty($firstname))) {
echo '<script language="javascript">alert("First name is wrong"</script>';
}
if((!preg_match("/^[a-zA-Z ]*$/",$lastname)) || (empty($lastname))) {
echo '<script language="javascript">alert("Last name is wrong")</script>';
}
if((!preg_match("/^[a-zA-Z]{3}[0-9]{3}$/",$stdid)) || (empty($stdid))) {
echo '<script language="javascript">alert("Studend id format is wrong")</script>';
}
if((!preg_match("/^[0-9]{1}(?:\.[0-9]{1})?$/",$gpa)) || (empty($gpa))) {
echo '<script language="javascript">alert(" Enter valid GPA")</script>';
}
if ( isset($firstname) && isset($lastname) && isset($stdid) && isset($gpa) )
{
$txt= $lastname.":".$firstname.":".$stdid.":".$gpa;
file_put_contents( $file,$txt."\n",FILE_APPEND);
exit();
}
fwrite($file_open,$txt);
natsort(file($file));
fclose($file_open);
}
?>