dongle2627 2017-07-10 12:06
浏览 43

Codeigniter Flash数据模态窗口中的消息:已编辑

I have a View page company_view which displays all the information of a college, has its own controller company.php to fetch results from the DB via the model.

Added a Modal Form to this page where the user inputs his details and it sends that data to the DB. In other words a Custom registration form.

http://startcodeigniter.blogspot.in/2015/12/create-simple-contact-form-in.html

But the above form is inside a modal.

The Insertion Process succeeds but I want the Success message to be displayed in the modal itself, it redirects into another page => (the new controller page contactform/index ) after submitting the form. Went through a few solutions to fix it , but nothing seemed to work out.

Is there any AJAX way to submit the data to the controller which prevents it from redirecting? Been scouring for similar AJAX example, still no luck.

What i was looking for is when the user submits the data, the modal displays that the data is submitted without the redirection.

Any help / Lead would help me a great deal!

Updated View - company_view.php

<button type="button" class="btn btn-info btn-lg btn-block" data-toggle="modal" data-target="#enquire">Enquire</button>


    <div class="modal fade" id="enquire">
    <div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title">Apply for "<?php echo $row_company->company_name;?> "</h4>
        </div>
        <div class="modal-body">
            <?php $attributes=a rray( "name"=> "contactform"); echo form_open("contactform/index", $attributes);?>
            <div class="form-group">
                <label for="name">Name</label>
                <input class="form-control" name="name" placeholder="Your Full Name" type="text" value="<?php echo set_value('name'); ?>" />
                <span class="text-danger"><?php echo form_error('name'); ?></span>
            </div>

            <div class="form-group">
                <label for="email">Email ID</label>
                <input class="form-control" name="email" placeholder="Email-ID" type="text" value="<?php echo set_value('email'); ?>" />
                <span class="text-danger"><?php echo form_error('email'); ?></span>
            </div>

            <div class="form-group">
                <label for="subject">Subject</label>
                <input class="form-control" name="subject" placeholder="Subject" type="text" value="<?php echo set_value('subject'); ?>" />
                <span class="text-danger"><?php echo form_error('subject'); ?></span>
            </div>

            <div class="form-group">
                <label for="message">Message</label>
                <textarea class="form-control" name="message" rows="4" placeholder="Message">
                    <?php echo set_value( 'message'); ?>
                </textarea>
                <span class="text-danger"><?php echo form_error('message'); ?></span>
            </div>

            <div class="form-group">
                <button name="submit" type="submit" class="btn btn-success">Submit</button>
            </div>
            <?php echo form_close(); ?>
            <?php echo $this->session->flashdata('msg'); ?>
        </div>
    </div>
    </div>
    </div>

Controller for form - contactform.php

<?php
class Contactform extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form','url'));
        $this->load->library(array('session', 'form_validation'));
        $this->load->database();
    }

    function index()
    {
        //set validation rules
        $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|callback_alpha_space_only');
        $this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
        $this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean');
        $this->form_validation->set_rules('message', 'Message', 'trim|required|xss_clean');

        //run validation on post data
        if ($this->form_validation->run() == FALSE)
        {   //validation fails
            $this->load->view('company_view',$data);
        }
        else
        {
            //insert the contact form data into database
            $data = array(
                'name' => $this->input->post('name'),
                'email' => $this->input->post('email'),
                'subject' => $this->input->post('subject'),
                'message' => $this->input->post('message')
            );

            if ($this->db->insert('contacts', $data))
            {
                // success
                $this->session->set_flashdata('msg','<div class="alert alert-success text-center">We received your message! Will get back to you shortly!!!</div>');
                redirect('contactform/index');
            }
            else
            {
                // error
                $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Some Error.  Please try again later!!!</div>');
                redirect('contactform/index');
            }
        }
    }

    //custom callback to accept only alphabets and space input
    function alpha_space_only($str)
    {
        if (!preg_match("/^[a-zA-Z ]+$/",$str))
        {
            $this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
}
?>
  • 写回答

2条回答 默认 最新

  • douzhe9927 2017-07-10 12:28
    关注

    There is a unorthodox method I have used.

    Put your modal in your html page with your flashdata like.

     <div class="modal fade" id="messageModal" role="dialog">
     <?php echo $this->session->flashdata('msg'); ?> 
    </div>
    

    then in your script tag load that modal if flashdata is set

    <?php if(!empty($this->session->flashdata('msg')) { ?>
     <script>
        $(window).on('load',function(){
           $('#messageModal').modal('show');
        });
     </script>
     <?php } ?>
    

    This might not be the best answer.But definitely a way to do this. All suggestions or improvements are welcome/welcomed

    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路