在学校项目中需要帮助!
我正在使用html / php,ssms和一些javascript开发Web应用程序。我已经做了一个表格,连接也正在工作。 多选下拉菜单的前端正在工作,但未将数据保存到数据库中。 尽管其他输入字段已保存,但多重选择未保存。我不确定是怎么了。 有人可以帮忙吗?
<html>
<body>
<div class="container" >
<form class="cmxform" id="Form">
<div id="FormResult" class="hide" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<div id="resultFormContent"></div>
</div>
<div class="form-group col-md-12">
* Indicates required field
</div>
<div class="form-row">
<div class="form-group col-md-4">
<label for="fName">First Name *</label>
<input type="text" class="form-control" id="fName" name="fName" required>
</div>
<div class="form-group col-md-4">
<label for="mName">Middle Name</label>
<input type="text" class="form-control" id="mName" name="mName" >
</div>
<div class="form-group col-md-4">
<label for="lName">Last Name *</label>
<input type="text" class="form-control" id="lName" name="lName" required>
</div>
</div>
<div class="form-group col-md-4">
<label for="race">Race</label>
<select class="form-control" multiple data-actions-box="true" id="race" name="race">
<option value="White">White</option>
<option value="Black">Black</option>
<option value="American Indian or Alaska Native ">American Indian or Alaska Native</option>
<option value="Asian">Asian</option>
<option value="Native Hawaiian or Other Pacific Islander">Native Hawaiian or Other Pacific Islander</option>
</select>
</div>
<div class="form-group col-md-4">
<label for="gender">Gender Identity</label>
<select name="gender" class="form-control" id="gender" >
<option value="">Select...</option>
<option value="Male/Man">Male/Man</option>
<option value="Female/Woman">Female/Woman</option>
<option value="TransMale/TransMan">TransMale/TransMan</option>
<option value="TransFemale/TransWoman">TransFemale/TransWoman</option>
<option value="GenderQueer/Gender nonconforming">GenderQueer/Gender nonconforming</option>
<option value="Something Else">Something Else</option>
<option value="Decline to answer">Decline to answer</option>
</select>
</div>
<div class="form-group col-md-12">
<label for="comments">Questions/Comments:</label>
<textarea class="form-control" id="comments" name="comments" rows="3"></textarea>
</div>
<!-- <div class="g-recaptcha" data-sitekey="6LfvDD8UAAAAAK8yM3oUhRmEGnQFrd49cLgJ_ypn" data-callback="captchaEvent"></div>-->
<br>
<div class="form-group col-md-12">
<button class="btn btn-info btn-primary" id="registerSubmit" type="submit">Submit</button>
</div>
</form>
</div>
</body>
</html>
功能
require_once 'queries.php';
$fName = filter_input(INPUT_POST, "fName");
$lName = filter_input(INPUT_POST, "lName");
$mName = filter_input(INPUT_POST, "mName");
$race = filter_input(INPUT_POST, "race");
$gender = filter_input(INPUT_POST, "gender");
$comments = filter_input(INPUT_POST, "comments");
switch ($switch) {
case 'add':
echo Queries::addInfo($fName, $lName, $mName, $race,$gender,$comments);
break;
}
?>
查询
<?php
require_once 'db_connection.php';
class Queries {
public static function addInfo($fName, $lName, $mName, $race,$gender,$comments ) {
$conn = DB::databaseConnection();
$conn->beginTransaction();
$sql = "INSERT INTO dbo.tracker (First_Name, Last_Name, Middle_Name, Race, Gender Identity, Comments) VALUES (:fName, :lName, :mName, :race,:gender,:comments )";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':fName', $fName);
$stmt->bindParam(':lName', $lName);
$stmt->bindParam(':mName', $mName);
$stmt->bindParam(':race', $race);
$stmt->bindParam(':gender', $gender);
$stmt->bindParam(':comments', $comments);
if ($stmt->execute()) {
$conn->commit();
return true;
} else {
$conn->rollback();
return false;
}
}
}
js
$(document).ready(function () {
$("#race").select2({
maximumSelectionLength: 5,
placeholder: "Select all that apply...",
allowClear: true
});
$("#Form").submit(function (event) {
event.preventDefault();
$.ajax({
url: 'functions/form-functions.php',
type: 'POST',
data: {"fName": $('#fName').val(), "lName": $('#lName').val(), "mName": $('#mName').val(),
"race": $('#race').val(), "gender": $('#gender').val(), "comments": $('#comments').val(), "switch": "add"},
dataType: "json",
success: function () {
$('#Form').trigger('reset');
resultAlert('#FormResult', '#resultFormContent', '<strong>Your data was succefully saved!</strong>', 'alert-success');
}, error: function () {
resultAlert('#FormResult', '#resultFormContent', '<strong>Error! Your data was not saved!</strong>', 'alert-danger');
}
});
});