好吧,我一直在努力,存储用户在输入文本中写入的内容,并存储用户点击的复选框的ID。我实现了存储两个东西,但每一个分开,更好的说在其他PHP文件。当我尝试仅使用一个php文件来处理这两件事时出现问题。这是我的代码
HTML
<label for="Teachers_Name">Teacher's Name</label>
<input type="text" name="Teachers_Name" id="Teachers_Name" placeholder="Name" required>
<br>
<br>
<label for="School_Name">School Name</label>
<input type="text" name="School_Name" id="School_Name" placeholder="School Name" required>
<br>
<br>
<label for="Implementation_Quality">Write here your
Implementation Quality score :</label>
<input type="text" name="Implementation_Quality" id="Implementation_Quality" placeholder="Implementation Quality" required>
</div>
<input type="submit" name="submit" id="submit" value="Send"
style="margin-left: 50px; margin-bottom: 20px; margin-top:
20px;background:#0774D9; color: #fff; font-size: 20px;
border-radius: 10px; font-family: 'Verdana';padding: 5px
15px 5px 20px">
<p>BOOKS</p>
<div class="checkbox">
<br>
<input type="checkbox" value="20" id="CourseBooks" class="get_value" style="display: none;">
<label for="CourseBooks">CourseBooks</label>
<input type="checkbox" value="20" id="PracticeBooks" class="get_value" style="display: none;">
<label for="PracticeBooks">PracticeBooks</label>
<br>
</div>
<h4 id="result"></h4>
Jquery的
<script>
$(document).ready(function() {
$('#submit').click(function() {
var insert = [];
$('.get_value').each(function() {
if ($(this).is(":checked")) {
insert.push($(this).attr("id"));
}
});
insert = insert.toString();
$.ajax({
url: "insert1.php",
method: "POST",
data: {
insert: insert
},
success: function(data) {
$('#result').html(data);
}
});
});
});
</script>
复选框的Php
if(isset($_POST['insert'])) {
$conn = mysqli_connect("localhost", "root", "", "datos1");
$query = "INSERT INTO BOOKS(name) VALUES('" . $_POST["insert"] . "') ";
$result = mysqli_query($conn, $query);
echo "Data Inserted Succesfully";
}
PHP文本
$conexion = new mysqli("localhost", "root", "");
if(!$conexion) {
echo "Conexión no exitosa";
} else {
$base = mysqli_select_db($conexion, "datos1");
if(!$base) {
echo "No se pudo conectar a la base de datos";
}
}
//LLAMAMOS LAS VARIBALES
$Teachers_Name = "";
$Teachers_Name = isset($_POST['Teachers_Name']) ? $_POST['Teachers_Name'] : '';
$Teachers_Name = empty($_POST['Teachers_Name']) ? $_POST['Teachers_Name'] : '';
$Teachers_Name= $_POST['Teachers_Name'] ?? '';
$School_Name = "";
$School_Name = isset($_POST['School_Name']) ? $_POST['School_Name'] : '';
$School_Name = empty($_POST['School_Name']) ? $_POST['School_Name'] : '';
$School_Name= $_POST['School_Name'] ?? '';
$Implementation_Quality = "";
$Implementation_Quality = isset($_POST['Implementation_Quality']) ? $_POST['Implementation_Quality'] : '';
$Implementation_Quality = empty($_POST['Implementation_Quality']) ? $_POST['Implementation_Quality'] : '';
$Implementation_Quality= $_POST['Implementation_Quality'] ?? '';
//Guarda las variables
$sql = "INSERT INTO datos_1 (Teachers_Name, School_Name,
Implementation_Quality) VALUES(?, ?, ?)";
$sth = mysqli_prepare($conexion, $sql);
mysqli_stmt_bind_param($sth, 'sss', $Teachers_Name, $School_Name, $Implementation_Quality);
$ejecutar = mysqli_stmt_execute($sth);
if(!$ejecutar) {
echo "Hubo algun error";
} else {
echo "Datos guardados correctamente<br><a href='index.php'>Volver</a>";
}