我检查了其他类似的问题,但我找不到一个解决办法,帮助我。
我正在尝试从数据库中获取数据,并将它们插入到添加到数据表的两个不同输入(复选框类型)中。我希望这两个输入包含在我的表单中,这会将信息发送到一个.php文件,并将其放置在数据库的另一个表中。我可以将“原始”表单的数据发送到.php文件,但是在查找复选框输入的值时遇到了问题。
$(document).ready(function(){
$.ajax ({
url: "dati_education.php",
metod: "POST",
dataType: "json",
success: function(data) {
$("#education").dataTable({
data: data,
columns: [
{ 'data' : 'Title' },
{ 'data' : 'Year' },
{ 'data' : "Place" },
{ 'data' : ' ',
'render': function(data, type, row){
return "<input class='check' id = '"+row.education_id+"'name='edu_id' type='checkbox' onclick= 'return "+ row.education_id + "'/>";
}
}
],
paging: true
})
}
});
$.ajax ({
url: "dati_work.php",
metod: "POST",
dataType: "json",
success: function(data) {
$("#work").dataTable({
data: data,
columns: [
{ 'data' : 'Company' },
{ 'data' : 'Role' },
{ 'data' : 'Year' },
{ 'data' : 'Place'},
{ 'data' : ' ',
'render': function(data, type, row){
return "<input class='chk'name='work_id' type='checkbox' onclick='return "+ row.work_id + "'/>";
}
}
],
paging: true
})
}
});
});
var id1 = 0;
$('#edu_id').on('click', function(){
id1 = $(".check:checked").each(function(){
$(this).find('input[type="checkbox"]').val();
});
});
//I add some console.log() to do some tests.
var id2= 0;
var work =[];
console.log('ciao');
$('#work_id').on('click', function(){
console.log('ciao');
$(".chk:checked").each(function(){
var id2 = $(this).attr("id");
//var id =$(this).find('input[type="checkbox"]').val();
console.log(id2);
work = JSON.stringify(id2);
work[0] = id2;
});
});
var data = $("#Form").serializeArray();
console.log(id1);
console.log(work);
data.push({
name : "edu_id",
value: id1
});
data.push({
name : "work_id",
value: work
});
console.log(data);
$("#Form").on("submit", function(){
$.ajax({
type: "POST",
url: "create_cards.php",
data: data,
success: function(data){
alert(data);
//alert('Finished! Starting over!');
}
});
});
如您所见,我尝试使用两种不同的方法来获取复选框的值,但两种方法都不起作用。我认为使用“serializearray”方法是正确的,因为从控制台我可以看到它添加了字段。但我仍然无法从复选框中获取值
以下是我的html代码:
<div class="container">
<div class="row">
<form id="Form" class="form-horizontal" action="create_cards.php" method="POST">
<div class="col-sm-6">
<br> <br> <br> <br> <br>
<div class="form-group">
<label class="control-label col-sm-2" for="title">Title:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="title" placeholder="Enter the Title" name="title">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="name">Name:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" placeholder="Enter your Name" name="name">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="surname">Surname:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="surname" placeholder="Enter your Surname" name="surname">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="email">Email:</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" placeholder="Enter your Email" name="email">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="phone">Phone:</label>
<div class="col-sm-10">
<input type="phone" class="form-control" id="phone" placeholder="Enter your Phone" name="phone">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="photo">Photo:</label>
<div class="col-sm-10">
<input type="select" class="form-control" id="photo" placeholder="Enter your Photo" name="photo">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="note">Note:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="note" placeholder="Enter some Note" name="note">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</div>
<div class="col-sm-6">
<table class="table table-hover table-border" id="education">
<thead>
<tr>
<th>Title</th>
<th>Year</th>
<th>Place</th>
<th> </th>
</tr>
</thead>
<tbody>
<td></td>
</tbody>
</table>
<table class="table table-hover" id="work">
<thead>
<tr>
<th>Company</th>
<th>Role</th>
<th>Year</th>
<th>Place</th>
<th> </th>
</tr>
</thead>
</table>
</div>
</form>
</div>
下面是一个文件,我从中获取要插入datatable的数据(另一个完全相同):
<?php
include("database.php");
$conn = mysqli_connect($db_host, $db_user, $db_password);
mysqli_select_db($conn, $db_database);
session_start();
$query = "select * from education_experience where user_id = '"
.$_SESSION["user_id"] ."'";
$result = mysqli_query($conn, $query);
$dati = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$dati[] = array(
"Title" => $row['title'],
"Year" => $row['year'],
"Place" => $row['place'],
"education_id" => $row['education_experience_id']
);
}
echo json_encode($dati);
?>
这是我要发送数据的文件。我还没有写查询,因为我想先解决这个问题。它不识别索引'edu\u id'和'work\u id'。
<?php
include("database.php");
$conn = mysqli_connect($db_host, $db_user, $db_password);
mysqli_select_db($conn, $db_database);
session_start();
echo "CIAO";
if(isset($_POST['title']) && isset($_POST['name']) &&
isset($_POST['surname']) && isset($_POST['email']) && isset($_POST['phone']) ) {
$Title = mysqli_escape_string($conn, $_POST['title']);
$Name = mysqli_escape_string($conn, $_POST['name']);
$Surname = mysqli_escape_string($conn, $_POST['surname']);
$Email = mysqli_escape_string($conn, $_POST['email']);
$Phone = mysqli_escape_string($conn, $_POST['phone']);
$Photo = mysqli_escape_string($conn, $_POST['photo']);
$Note = mysqli_escape_string($conn, $_POST['note']);
$Edu_id = mysqli_escape_string($conn, $_POST['edu_id']);
$Work_id = mysqli_escape_string($conn, $_POST['work_id']);
echo $Title, $Name, $Edu_id, $Work_id;
}
?>
我对这个问题还很陌生,所以请友善一点。也很抱歉可能出现语法错误,但英语不是我的母语。
暂无答案!
目前还没有任何答案,快来回答吧!