【问题标题】:Codeigniter return a message to view from controller troubleCodeigniter 从控制器故障返回消息以查看
【发布时间】:2015-03-04 20:53:11
【问题描述】:

当我使用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);

}

【问题讨论】:

  • add_message() 是内部 Codeigniter 函数吗?它是在哪里定义的?
  • 我想您正在寻找一条消息?
  • 我已经在另一个函数中使用了 add_message() 并且效果很好。
  • 我看起来像那样(闪信)。

标签: php codeigniter model-view-controller


【解决方案1】:

你的模型需要返回一些东西。尝试像这样编辑您的模型

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>

【讨论】:

  • 那么你想对这条消息做什么?据我所知,Codeigniter 中没有内部add_message() .. 你想用这条消息做什么,可能会以其他方式帮助你实现你的目标
  • 我在发送数据时试图获得一种警报
  • 你明白我做了什么吗?
  • 我知道你做了什么,我不知道在视图中做什么,对不起,我真的是 codeigniter 的新手。如果你能解释一下如何处理视图中的数据,请
  • 我在答案中添加了另一行。由于您将一个名为 $data 的数组传递给您的视图。您的视图会将 $data 数组中的每个键转换为变量。您的 $data 数组的键为 "message" ,因此视图会将其视为名为 $message 的变量
【解决方案2】:

如果您想按您所说的那样添加警报,那么使用 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";
        }
    }
}

【讨论】:

  • 我不明白另一个 HTTP 请求只是为了显示一条消息。几秒钟后显示消息并重定向不是更好吗?
  • 好吧,他想要的结构如下:在他的视图中开始->提交表单(HTTP Req)->保存到数据库->如果成功警报并转到其他页面->如果错误仍然存​​在在页面上..“另一个 HTTP 请求”是什么意思?
  • 非常感谢 Godie,我用 Javascript 做到了,但你帮了我很多。再次感谢您
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多