在php中使用javascript加载模态(CodeIgniter)

时间:2017-06-22 06:50:15

标签: php jquery mysql ajax codeigniter

我想在成功提交表单时打开模型,但是虽然表单提交模式没有弹出。

public function insert($data) {
    // Inserting into your table
    // Calling model
    $done = $this->db->insert('sign_up', $data);

    if($done) {
        echo "<script>$('#thankyouModal').modal('show')</script>";
        echo '<div class="modal fade" id="thankyouModal" tabindex="-1" role="dialog">
        <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" id="myModalLabel">Thank you for pre-registering!</h4>
                </div>
                <div class="modal-body">
                    <p>Thanks for getting in touch!</p>                     
                </div>    
            </div>
        </div>
    </div>';
    }
}

我正在尝试使用

在控制器中打开模型
   $data = array(
            'first_name' => $fname,
            'last_name' => $lname,
            'email' => $email,
            'password' => $password
        );
        $this->load->model('modal');
        $this->modal->insert($data);

3 个答案:

答案 0 :(得分:0)

尝试以下代码;这将有效

public function insert($data) {
    // Inserting into your table
    // Calling model
    $done = $this->db->insert('sign_up', $data);

    // You can do something else here
    if($done) {
?>
        <script>$(document).ready(function(){$('#thankyouModal').modal('show')});</script>
        <div class="modal fade" id="thankyouModal" tabindex="-1" role="dialog">
        <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" id="myModalLabel">Thank you for pre-registering!</h4>
                </div>
                <div class="modal-body">
                    <p>Thanks for getting in touch!</p>                     
                </div>    
            </div>
        </div>
    </div>
<?php
    }
}

答案 1 :(得分:0)

将模态代码放在视图文件中,以便我们可以在视图文件中使用javascript轻松调用模态。 并且您需要在Flash数据中设置一个变量,以便您可以检查表单是否已成功插入。 这是代码,我希望它可以帮助你:

控制器文件:

    public function insert($data) {
    // Inserting into your table
    // Calling model
    $done = $this->db->insert('sign_up', $data);
    // You can do something else here
    if($done) {
      //You can set the message and variable name as per your need.
      $this->session->set_flashdata('inserted','Yes');
      //Redirect to the desired view file
      redirect("controller/anotherfunction_where_view_file_is_loaded");
    }

查看文件:

 <div class="modal fade" id="thankyouModal" tabindex="-1" role="dialog">
    <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" id="myModalLabel">Thank you for pre-registering!</h4>
            </div>
            <div class="modal-body">
                <p>Thanks for getting in touch!</p>                     
            </div>    
        </div>
    </div>
</div>

<?php if((isset($this->session->flashdata('inserted'))) && $this->session->flashdata('inserted') != "") { ?>

<script type="text/javascript">

    $(document).ready(function(){
      $('#thankyouModal').modal('show');
    });

</script>

<?php } ?>

答案 2 :(得分:0)

我得到了答案

CONTROLLER

public function insert() {
    $signup_email = $this->input->post('signup_email');
    $signup_password = $this->input->post('signup_password');
    // Checking if everything is there
    if ($fname && $signup_email && $signup_password) {
        $data = array(
              'email' => $signup_email,
              'password' => $signup_password
        );
        if($this->modal->insert($data)) {
             echo "SOME DATA TO BE DISPLAYED IN MODAL";
        }
     }
}

查看

<body>

<div class="modal fade" id="thankyouModal" tabindex="-1" role="dialog">
    <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" id="myModalLabel">Thank you for pre-registering!</h4>
            </div>
            <div class="modal-body">
                <p>Thanks for getting in touch!</p>                     
            </div>    
        </div>
    </div>
</div>

<script>
     var signup_email = $('#signUp').val();
     var signup_password = $('#signUpPassword').val() ;                  
                $.ajax({
                    type: "POST",
                    url: "<?php echo site_url('form/insert'); ?>",
                    data: { signup_email: signup_email, signup_password: signup_password },
                    dataType: "html",
                    success: function(data) {
                        if(data) {
                            $('#thankyouModal').modal('show');
                        }
                    }, error: function() {
                        alert("ERROR!");
                    }
                });

</script>

</body>