如何在Jquery中将数据发布到数据库

时间:2013-12-20 17:08:47

标签: javascript php jquery

目前我有这个jquery可以帮助我从我的数据库中获取数据,该数据只显示选择此部门时的不同类型的选项。但现在我想再次将这些数据发布到数据库。有什么解决方案吗?这是我的代码:

<script type="text/javascript" src="js/jquery-1.7.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#cat").change(function() {
                $.ajax({
                    type: "GET",
                    url: "getPositionTitle.php",
                    data: "category_id=" + $(this).find(":selected").val(),
                    cache: false,
                    success: function(msg){
                        $("#position_title").empty();
                        my_position_title_array = $.parseJSON(msg);
                        for (i = 0; i < my_position_title_array.length; i ++) {
                            $("#position_title").append('<option value="' + my_position_title_array[i].id + '">' 
                                + my_position_title_array[i].position_title + '</option>');
                        }
                        $("#position_title").trigger('change');
                    }
                });
            });
            $("#cat").trigger('change');
        });   
    </script>


<form id="offeredjob" method="post" action="doOfferedJob.php">

<tr>
     <td><label for="applied_department">Department:</label></td>
     <td>
         <select id="cat" name ="applied_department" applied_position_title="category">
          <?php
                $query = "SELECT id, department FROM department";
                $result = mysqli_query($link, $query) or die(mysqli_error());
                    while ($row = mysqli_fetch_assoc($result)) {
                      echo "<option value ='" . $row['id'] . "'>" . $row['department'] . "</option>";
                            }
                            ?>
                        </select>

                    </td>
                </tr>
                <tr>
                    <td><label for = "applied_position_title">Position Title:</label></td>
                    <td>
                        <select id="position_title" applied_position_title="applied_position_title">
                            <option value="1"></option>
                        </select>
                    </td>
                </tr>

这就是我发布到我的数据库的方式:

$query = "UPDATE job_application SET applied_department = '$applied_department', applied_position_title = '$applied_position_title' WHERE id = '$id'";

$result = mysqli_query($link, $query) or die(mysqli_error($link));

1 个答案:

答案 0 :(得分:0)

将这样的内容添加到您的JQuery中。

// Post data to postPositionData.php when user changes form
$("#offeredjob").change(function() {
    // Serialize form data
    var yourFormData = $(this).serialize();
    // POST
    $.ajax({
        type: "POST",
        url: "postPositionData.php",
        data: yourFormData,
        success: function(msg){
            // do something
        }
    });
});

文件postPositionData.php然后将进行数据库插入/更新。