<?PHP
//$errorMessage = "";
//$check = "";
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
//===================================================
// GET THE QUESTION AND ANSWERS FROM THE FORM
//===================================================
$sID = $_POST['studentID'];
$sID = htmlspecialchars($sID);
$firstName = $_POST['firstName'];
$firstName = htmlspecialchars($firstName);
$lastName = $_POST['lastName'];
$lastName = htmlspecialchars($lastName);
$grade = $_POST['grade'];
$grade = htmlspecialchars($grade);
//var_dump($grade);
//============================================
// OPEN A CONNECTION TO THE DATABASE
//============================================
$user_name = "root";
$password = "";
$database = "surveyTest";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
//============================================
// GET THE LAST QUESTION NUMBER
//============================================
$SQL = "Select * FROM students WHERE SID='$sID'";
$result = mysql_query($SQL);
$db_field = mysql_fetch_assoc($result);
$studentID = $db_field['SID'];
var_dump($studentID);
//=========================================================
// Add a student to the students TABLE
//=========================================================
$SQL = "INSERT INTO students (SID, fName, lName, Grade) VALUES ('$sID', '$firstName', '$lastName', '$grade')";
$result = mysql_query($SQL);
//=============================================================
// SET Multiple rows IN THE answers TABLE for each question for a given student.
//=============================================================
/*$SQL = "Select * FROM tblquestions";
$result = mysql_query($SQL);
$numRows = mysql_num_rows($result); //return number of rows in the table
for ($i = 1; $i <= $numRows; $i++){
$qNum = 'q1';
$SQL = "INSERT INTO answers (QID, A, B, C, D, E, SID) VALUES ('$qNum', 0, 0, 0, 0, 0, '$sID')";
$question_Number = ltrim($qNum,'q');
$question_Number++;
$qNum ='q'.$question_Number;
$result = mysql_query($SQL);
}*/
mysql_close($db_handle);
print "The student with the following ID ".$sID. " has been added to the database";
}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}
}
?>
<html>
<head>
<title>Survey Admin Page</title>
</head>
<body>
<FORM NAME ="setQuestionForm" METHOD ="POST" ACTION ="setStudent.php" id="sStudent">
<p>Enter student ID: <INPUT TYPE = 'TEXT' Name ='studentID' size="4"></p>
<p>Enter First Name: <input type="text" name="firstName" size="20"></p>
<p>Enter Last Name: <input type="text" name="lastName" size="20"></p>
<p>Select Grade: <select name = "grade">
<option value = "1">First Grade</option>
<option value = "2">Second Grade</option>
<option value = "3">Third Grade</option>
<option value = "4">Fourth Grade</option>
<option value = "5">Fifth Grade</option>
<option value = "6">Sixth Grade</option>
<option value = "7">Seventh Grade</option>
<option value = "8">Eighth Grade</option>
</select></p>
<INPUT TYPE = "submit" Name = "Submit1" VALUE = "Add Student">
</FORM>
<P>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
$(function(){
if ($('form').length > 0) {
$('form').submit(function(e){
var check = "<?php echo $studentID; ?>";
alert(check);
if (check != "")
{
alert ("This user already exists");
return false;
}
else
{
return true;
}
});
}
})
</script>
</body>
</html>
上面的代码用于将学生添加到数据库中,我正在尝试进行一些表单验证以避免重复记录。我的问题是我设置了一个 php 变量 $studentID 来验证数据库是否包含具有相同 ID 的学生。
但是,当我尝试添加重复记录时,似乎我的 javascript 代码首先执行,这可以通过 JQuery 代码中的警报消息观察到,它显示一个空字符串框。再次执行代码,做正确的事。
关于如何解决这个问题的任何帮助?