Codeigniter返回一条消息,以便从控制器故障中查看

时间:2015-03-04 20:53:12

标签: php codeigniter model-view-controller

当我使用add_message("message",'success/danger');在我的数据库中插入数据时,我正试图推送消息即使查询在数据库中添加数据,我也没有得到任何信息。

控制器:

function add_nino() {

    $id_representante_submit = (int) $this->input->post('id_representante', TRUE);

    if ($id_representante_submit > 0) {

        $representante_data['id_representante'] = $this->input->post('id_representante', TRUE);
        $representante_data['id_nino'] = $this->input->post('nino', TRUE);
        $representante_data['id_parentesco'] = $this->input->post('parentesco', TRUE);

        $result = $this->representante->add_nino($representante_data);

        if($result){
            add_message("Test", 'success');
        } else {
            add_message("Test1", 'danger');
        }
    }
    $this->load->view('representante/add_nino_repre');
}

型号:

function add_nino($representante_data) {

    $this->db->insert('nino_padre', $representante_data);

}

2 个答案:

答案 0 :(得分:0)

你的模特需要归还一些东西。尝试编辑模型,如下所示:

function add_nino($representante_data) {
    $this->db->insert('nino_padre', $representante_data);
    if($this->db->affected_rows() > 0){
        return true;
    } else {
        return false;
    }
}

更新:和您的控制器

function add_nino() {

    $id_representante_submit = (int) $this->input->post('id_representante', TRUE);

    if ($id_representante_submit > 0) {

        $representante_data['id_representante'] = $this->input->post('id_representante', TRUE);
        $representante_data['id_nino'] = $this->input->post('nino', TRUE);
        $representante_data['id_parentesco'] = $this->input->post('parentesco', TRUE);

        $result = $this->representante->add_nino($representante_data);

        if($result){
            $data = array("message" => "success"); //sets variables in $data
        } else {
            $data = array("message" => "danger"); //sets variables in $data
        }
    }
    $this->load->view('representante/add_nino_repre', $data); //passes variables to view
}

查看:representante / add_nino_repre

<div>
    <?php
     if(isset($message)){
         echo $message;
     } 
    ?>
</div>

答案 1 :(得分:0)

如果您想要添加警报,则可能最好使用jquery:

你的JS在你看来:

$(document).ready(function() {

    $("#myForm").submit(function() {
        var url = "myapp.com/index.php/add_nino";
        var postdata = $(this).serialize();
        $.post(url, postdata, function(result) {
            alert(result);
            if (result === "success") {
                location.href = "some other url";
            }
        });
        return false;
    });
});

和您的控制人员:

function add_nino() {

    $id_representante_submit = (int) $this->input->post('id_representante', TRUE);

    if ($id_representante_submit > 0) {

        $representante_data['id_representante'] = $this->input->post('id_representante', TRUE);
        $representante_data['id_nino'] = $this->input->post('nino', TRUE);
        $representante_data['id_parentesco'] = $this->input->post('parentesco', TRUE);

        $result = $this->representante->add_nino($representante_data);

        if($result){
            echo "success";
        } else {
            echo "danger";
        }
    }
}