我目前正在开发一个网站,该网站从Bootstrap模式( addStudentModal.php )中的表单获取信息,并通过jQuery脚本将其发送到 createRecord.php ( dataManageFunctions.js )。当数据到达 createRecord.php 时,它将被插入到MySQL数据库中。出于调试目的,我只是让它回显出POST给它的变量,而不是执行INSERT命令。我发现除了2个字段外都没有发布,我完全不知道为什么。具体来说, gYear 和 lName 正在回显空白值。我检查了拼写并尝试确保变量名称和ID相同。也许我错过了一些东西,我不知道。
有问题的文件如下:
addStudentModal.php
<!-- ADD STUDENT Modal -->
<div class="modal fade" id="addStudentModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Add New Student</h4>
</div>
<div class="modal-body">
<div id="additionResults"></div>
<!-- EDIT form-->
<form role="form" action="" method="" id="addStudentForm">
<div class="form-group">
<label for="studentID">Student ID Number:</label>
<input type="text" class="form-control" name="studentID" id="studentID" placeholder="123456">
</div>
<div class="form-group">
<label for="lastName">Graduation Year:</label>
<input type="text" class="form-control" name="gradYear" id="gradYear" placeholder="0">
</div>
<div class="form-group">
<label for="lastName">Last Name:</label>
<input type="text" class="form-control" name="lastName" id="lastName" placeholder="Doe">
</div>
<div class="form-group">
<label for="firstName">First Name:</label>
<input type="text" class="form-control" name="firstName" id="firstName" placeholder="John">
</div>
<div class="form-group">
<label for="serHrsToDate">Service Hours to Date:</label>
<input type="text" class="form-control" name="serHrsToDate" id="serHrsToDate" placeholder="0.00">
</div>
<div class="form-group">
<label for="internToDate">Internships to Date:</label>
<input type="text" class="form-control" name="internToDate" id="internToDate" placeholder="0">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success" name="save" value="save"> Add Student </button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
<!-- END ADD STUDENT MODAL -->
dataManageFunctions.js - 仅复制相关代码段
// JS for Add Student
$(function() {
$("#addStudentForm").bind('submit',function() {
var sID = $('#studentID').val();
var gYear = $('#gradYear').val();
var lName = $('#lastName').val();
var fName = $('#firstName').val();
var servHrs = $('#serHrsToDate').val();
var interns = $('#internToDate').val();
$.post('assetScripts/editInfo/createRecord.php',{sID:sID, gYear:gYear, lName:lName, fName:fName, servHrs:servHrs, interns:interns}, function(data){
$("#additionResults").html(data);
});
return false;
});
});
createRecord.php
<?php
include("../../config/db-connect.php");
$errExist = false;
if(isset($_POST['sID'])){
$safeID = intval($_POST['sID']);
if(!preg_match("/^.*(?=.{6,})(?=.*[0-9]).*$/", $safeID)){
$errExist = true;
}
} else {
$errExist = true;
}
if(isset($_POST['gYear'])){
$safeYear = intval($_POST['gYear']);
if(!preg_match("/^.*(?=.{4,})(?=.*[0-9]).*$/", $safeYear)){
$errExist = true;
}
} else {
$errExist = true;
}
if(isset($_POST['lName'])){
$safeLastName = mysql_real_escape_string(filter_var($_POST['lName'], FILTER_SANITIZE_STRING));
} else {
$errExist = true;
}
if(isset($_POST['fName'])){
$safeFirstName = mysql_real_escape_string(filter_var($_POST['fName'], FILTER_SANITIZE_STRING));
} else {
$errExist = true;
}
if(isset($_POST['servHrs'])){
$safeHrs = floatval($_POST['servHrs']);
} else {
$errExist = true;
}
if(isset($_POST['interns'])){
$safeInterns = intval($_POST['interns']);
} else {
$errExist = true;
}
// check if an error existed when checking inputs
if(!$errExist){
echo '<div class="alert alert-success" role="alert"><strong>Success!</strong></div>';
echo $_POST['sID'].', '.$_POST['gYear'].', '.$_POST['lName'].', '.$_POST['fName'].', '.$_POST['servHrs'].', '.$_POST['interns'].'<br>';
echo $safeID.', '.$safeYear.', '.$safeLastName.', '.$safeFirstName.', '.$safeHrs.', '.$safeInterns.'<br>';
echo $errExist.'<br>';
} else {
echo '<div class="alert alert-danger" role="alert"><strong>Failure!</strong></div>';
echo $_POST['sID'].', '.$_POST['gYear'].', '.$_POST['lName'].', '.$_POST['fName'].', '.$_POST['servHrs'].', '.$_POST['interns'].'<br>';
echo $safeID.', '.$safeYear.', '.$safeLastName.', '.$safeFirstName.', '.$safeHrs.', '.$safeInterns.'<br>';
echo $errExist.'<br>';
}
?>
还值得注意的是,我还有回声 isset(变量名),它为每个变量返回1(真)。是的,我确保我输入的测试值遵循每个条件,因此 $ errExist 不应该设置为真。