通过JQuery将数据表中的Checkbox数据发送到php文件

时间:2018-06-08 22:06:58

标签: php jquery mysql ajax datatables

我检查了其他类似的问题,但我找不到帮助我的解决方案。

我正在尝试从数据库获取数据并将其插入到我的 dataTable 复选框类型) >。我希望这两个输入包含在我的表单中,并将信息发送到.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_id'和'work_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;
}
?>

我仍然对这个主题很新,所以请你好。 很抱歉可能存在语法错误,但英语不是我的母语。

0 个答案:

没有答案