【问题标题】:How to send an checkbox array and some other values with ajax jquery post through PHP MVC如何通过 PHP MVC 使用 ajax jquery post 发送复选框数组和其他一些值
【发布时间】:2015-07-20 11:38:30
【问题描述】:

我正在尝试使用 AJAX/JQUERY/PHP(MVC) 将数据发送到数据库;

我有一张包含大量数据的表格;该表包含用于检查可以发送到数据库的行的复选框。

对于查看文件;我试图发送这样的数据:

已编辑

<form action="<?php echo URL ?>etudiants/affectation" method="POST" id="affectStudents"  class='form-vertical'>
    <div class="span3">
        <div class="control-group">
            <div class="input-append">
                <select name="id_salle" id="id_salle" class='input-medium'>
                    <option value="0">«Choisir Salle»</option>
                    <?php foreach($this->fetchSalle AS $key => $value): ?>
                    <option value="<?php echo $value['id_salle'] ?>">
                    <?php echo $value['intitule_salle']; ?>
                    => C: <?php echo $value['capacite']; ?>
                    => M: <?php echo $value['marge']; ?>
                    </option>
                <?php endforeach; ?>
                </select>
                <button class="btn" type="submit"> Affecter</button>
            </div>
        </div>
    </div>
    <div class="box-content nopadding">
        <table class="table table-hover table-nomargin table-bordered dataTable dataTable-nosort" data-nosort="0">
            <thead>
                <tr class='thefilter'>
                    <th class='with-checkbox'>
                    <input type="checkbox" name="check_all" id="check_all"></th>
                    <th>Code Etud.</th>
                    <th>Filière</th>
                    <th>Nom et Prénom</th>
                    <th>CIN</th>
                    <th>Tel</th>
                    <th>Email</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach($this->fetchEtudiants AS $value): ?>
                <tr>
                    <td class="with-checkbox">
                    <input type="checkbox" name="id_etudiants[]" value="<?php echo $value['id_etudiant']; ?>" class="check_id" id="check_id">
                    </td>
                    <td><?php echo $value['code_etudiant']; ?></td>
                    <td><?php echo $value['intitule_filiere']; ?></td>
                    <td><?php echo $value['nom']; ?> 
                         <?php echo $value['prenom']; ?>
                    </td>
                    <td><?php echo $value['cin']; ?></td>
                    <td><?php echo $value['tel']; ?></td>
                    <td><?php echo $value['email']; ?></td>
                </tr>
            <?php endforeach; ?>    
        </tbody>
    </table>
    <input type="hidden" name="csem" value="<?php echo @$_GET['csem'] ?>">
    <input type="hidden" name="cses" value="<?php echo @$_GET['cses'] ?>">
    <input type="hidden" name="cfil" value="<?php echo @$_GET['cfil'] ?>">
    <input type="hidden" name="cmod" value="<?php echo @$_GET['cmod'] ?>">
</form>

控制器文件

etudiants.php

class Etudiants extends Controller {

    function __construct(){
        parent::__construct();          
    }
    public function index(){        
        $this->view->render('etudiants/index');
    }

    function affectation(){
        $this->model->affectation();
    }   
}

etudiants_model.php

class Etudiants_Model extends Model
    {
    public function __construct(){
        parent::__construct();
    }
    public function affectation(){
        $students[] = $_POST['id_etudiants'];
        $id_salle   = $_POST['id_salle'];

        for ($i=1; $i <= strlen($students) ; $i++) {
            $stmt = $this->db->prepare("INSERT INTO exam_salles (id_student, id_salle) VALUES(?,?)");
            $stmt->execute(array($students[$i], $id_salle));
        }

        if($stmt == TRUE){
            echo "good";
        }else{
            echo "wrong";
        }
    }
}

对于 JS 文件我使用了这段代码。

$(document).ready(function(){
$("#affectStudents").on('submit', function(e){
    e.preventDefault();
    var url     = $(this).attr('action');
    var data    = $(this).serialize();

    $.post(url, data, function(response) {
        if(response == 'good'){
            $("#affectedSuccessfully").show();
        }else{
            $("#affectedError").show();
        }           
    });                
});
});

当我点击按钮 SEND 时,它会将我重定向到 /etudiants/affectation,即使我使用的是 preventDefault(); 和不向数据库发送数据。

【问题讨论】:

  • console.log $("#affectStudents").确保在 DOM 准备好之后运行 JS。
  • 您发布的 HTML 显示 SEND 是文本字段,而不是按钮,您确定您发布的代码正确吗?
  • 我更新了代码,这里只是一个错字

标签: php jquery ajax database model-view-controller


【解决方案1】:

我相信您在创建表单之前加载了 javascript,因此事件处理程序在创建受影响的元素之前运行,因此它不会影响目标元素。您需要在页面加载后注册处理程序

$(function() {
    $("#affectStudents").on('submit', function(e){
        e.preventDefault();
        var url     = $(this).attr('action');
        var data    = $(this).serialize();

        $.post(url, data, function(response) {
            if(response == 'good'){
                $("#affectedSuccessfully").show();
            }else{
                $("#affectedError").show();
            }           
        }                 
    });
});

或者,您可以编写一个期望目标在未来存在的代码

$("body").on('submit', "#affectStudents", function(e){
    e.preventDefault();
    var url     = $(this).attr('action');
    var data    = $(this).serialize();

    $.post(url, data, function(response) {
        if(response == 'good'){
            $("#affectedSuccessfully").show();
        }else{
            $("#affectedError").show();
        }           
    }                 
});

【讨论】:

  • 是的,我知道,我正在使用 $(document).ready();我在这里只是一个错字
  • 我使用的这个js文件代码有问题,我在这个文件中有另一个函数,当我添加上面的js代码时它不起作用,但是当我删除上面的代码时,一切正常。好奇怪!!!
  • 那么,如果您删除了部分旧代码,那么它运行良好吗?
  • 不,当我删除我放在这里的这段代码时,用于其他目的的旧代码运行良好..这里的这个不起作用......我不知道为什么!
【解决方案2】:

你的JS文件加载好了吗?还有 jQuery 库?好像没搞定,你的js函数。 如果有错误,还要检查调试栏中的错误。

编辑(讨论后)

http://jsfiddle.net/wvwm5Luz/2/ 您的 jQuery 序列化方法将每个复选框作为一个变量。我认为你可以这样做:

使用不带名称的复选框。选中复选框(在 dataTable 插件中很容易,http://datatables.net/forums/discussion/2511/get-all-checked-checkboxes-in-the-datatable 应该会有所帮助)并将其添加到您的 post 中的 var data。值可以是 '1,3,8' 等格式,在 etudiants_model.php 中写如下内容:

$students = explode(',', $_POST['id_etudiants'];

之后我想它会起作用。

【讨论】:

  • JS文件加载完毕
  • 你有什么错误吗?你加载了哪个版本的jQuery?
  • 尝试在你的函数之前写alert($("#affectStudents"));。你有 undefined 或 htmlelm 或类似的东西吗?或者在同一个地方尝试console.log($("#affectStudents")); 并将您写入的内容写入控制台。您还可以在您的问题中添加哪些代码在同一位置工作并且在您的 on(submit) 函数之后不起作用。我们可以看看差异。
  • 我认为问题出在复选框输入数组上,当我删除它时,一切正常。我对上面的代码,复选框做错了吗?我相信当有多个复选框时,我们应该将名称设为数组,这样,name="checkbox[]" 并循环遍历它以将 POST 值存储到数据库中,就像我在上面的代码中所做的那样?
  • 我认为复选框没有任何问题。我在这里试过:jsfiddle.net/wvwm5Luz 但是,序列化后复选框的值很奇怪......名称中的数组在 PHP 中运行良好。你的代码有什么意义?
猜你喜欢
  • 2012-09-08
  • 1970-01-01
  • 2011-09-23
  • 1970-01-01
  • 1970-01-01
  • 2012-03-18
  • 1970-01-01
  • 1970-01-01
  • 2021-06-01
相关资源
最近更新 更多