提交后显示警告消息

时间:2016-01-27 03:35:14

标签: php jquery forms codeigniter

我在提交表单后出现问题时遇到问题。 我有一个提交报告的表格,然后在提交后将其存储在一个表格中。我想要发生的是,在提交报告后,每个新提交的报告都会有一条警报消息。我正在使用CI作为框架。这就是我所拥有的:

查看:

<div class="row-fluid">
                    <button type="button" class="btn btn-primary col-lg-2" data-toggle="collapse" data-target="#add_form" onclick="add_clicked()">Submit Report</button><br />

                <div class="col-lg-6">
                    <?php
                    $attributes = array('class' => $class, 'id' => 'add_form', 'role' => 'form');
                    echo form_open('admin/report', $attributes);?>
                    <div class="form-group">
                        <input class="form-control" type="hidden" name="add_hidden" value="add-report">
                    </div>
                    <br><br>
                    <div class="form-group">
                        <?=form_input( array('id'=>'sender','class'=>'form-control' ,'name'=>'sender','type'=>'text','placeholder'=> 'Senders Name', 'value' => set_value('sender'), 'required'=> 'true') );?> <br />
                        <?=form_input( array('id'=>'date','class'=>'form-control date-picker','name'=>'date','type'=>'text','placeholder'=> 'Date', 'value' => set_value('date') ) );
                        $date = date('Y-m-d H:i:s');?><br />
                        <?=form_textarea( array('id'=>'subject','class'=>'form-control','name'=>'subject','type'=>'message-text','placeholder'=> 'Your Message here', 'value' => set_value('subject'), 'required'=> 'true') );?><br />


                    </div>
                    <div class="form-group">
                        <?=form_input( array('class'=>'btn btn-success','id'=>'add_submit','type'=>'submit','value'=>'submit')) ;?>
                    </div>
                    <?=form_close();?>
                </div>
                <div class="col-md-10 text-center" id="success" style="display:none">
                    <?php if(isset($message)) echo $message ?>
                </div>
            </div>
        </div>
    <br> <br>
        <div class="table-responsive">
            <table class="table table-hover table-striped">
                <thead>
                <tr>
                    <th style="text-align: center">Sender</th>
                    <th style="text-align:center">Subject</th>
                    <th style="text-align:center">Date</th>


                </tr>
                </thead>
                <tbody>
                <?php
                foreach ($query->result_array() as $row) {

                    echo '
                                                                    <tr>
                                                                            <td style="text-align:center">'.$row["sender"].'</td>
                                                                            <td style="text-align:center">'.$row["subject"].'</td>
                                                                            <td style="text-align:center">'.$row["date"].'</td>

                                                                    <td> &nbsp; &nbsp; &nbsp; <button type="button" class="btn btn-success btn-small" data-toggle="modal" data-target="#view'.$row["sender"].'"> Read </button> &nbsp; &nbsp; &nbsp;
                                                                            <button type="button" class="btn btn-danger btn-small" data-toggle="modal" data-target="#remove'.$row["sender"].'">delete</button></td>


                                                                    </tr>




                                                                    ';
                }
                ?>

这是我的控制器:

public function report()
    {
        $this->load->model('report_model', 'database');
        $sql = "SELECT * FROM report";
        $data['query'] = $this->database->view_report($sql);

    if ($this->input->post('add_hidden')) {
        $this->form_validation->set_rules('sender', 'sender', 'required');
        $this->form_validation->set_rules('subject', 'subject', 'required');
        $this->form_validation->set_rules('date', 'date');

        $this->form_validation->set_error_delimiters('<div class="has-error" style="color: #FF0000">', '</div>');

        if ($this->form_validation->run() == TRUE) {

            //Setting values for table columns
            $data = array(
                'sender' => $this->input->post('sender'),
                'subject' => $this->input->post('subject'),
                'date' => $this->input->post('date'),


            );
            //Transfering data to Model
            $q = $this->database->add_report($data);
            $data['query'] = $this->database->view_report($sql);

            if ($q) {
                $data['message'] = '<h3> Your report has successfully submitted!</h3>';
                $data['success'] = '';
                $data['class'] = 'collapse';
                $this->load->view('report_view', $data);

            } else {
                $data['message'] = '<h3> Report submission unsuccessful!</h3>';
                $data['success'] = '';
                $data['class'] = 'collapse';
                $this->load->view('report_view', $data);
            }
        }
        else {
            $data['message'] = '';
            $data['success'] = 'none';
            $data['class'] = '';
            $this->load->view('report_view', $data);
        }

    }
    else if ($this->input->post('del_hidden')) {

        $from = $this->input->post('del_sender');
        $subject = $this->input->post('del_subject');
        $date = $this->input->post('del_date');

        $qry = "DELETE FROM report WHERE sender = '$from'";
        $q = $this->database->delete_report($qry);

        $sql = "SELECT * FROM report";
        $data['query'] = $this->database->view_report($sql);

        if ($q) {
            $data['message'] = '<h3> "' . $subject . '"message deleted from database!!</h3>';
            $data['success'] = '';
            $data['class'] = 'collapse';
            $this->load->view('report_view', $data);
        }

    } else {
        $data['success'] = 'none';
        $data['class'] = 'collapse';
        $this->load->view('report_view', $data);
    }

}`

2 个答案:

答案 0 :(得分:3)

实际上你正在视图中显示消息;这部分代码就足够了。

<div class="col-md-10 text-center" id="success" style="display:none">
   <?php if(isset($message)) echo $message ?>
</div>

但div的样式为display:none,因此不会显示。所以把它改成

<?php if(isset($message))
{
 ?>
    <div class="col-md-10 text-center" id="success">
       <?php  echo $message ?>
    </div>
<?php 
}
?>

如果您需要提醒信息,则可以将其更改为

<?php if(isset($message))
{
 ?>
    <script>
       alert('<?php  echo $message ?>');
    </script>
<?php 
}
?>

答案 1 :(得分:0)

如果是javascript警报,请使用提醒消息搜索警报表达式,然后输入。并使用console.log()...