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

Posted

技术标签:

【中文标题】在 php(CodeIgniter) 中不使用 javascript 加载模态【英文标题】:Modal not loading using javascript in php(CodeIgniter) 【发布时间】:2017-11-25 07:17:25 【问题描述】:

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

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);

【问题讨论】:

【参考方案1】:

试试下面的代码;这会工作

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
    

【讨论】:

【参考方案2】:

将modal的代码放在一个view文件中,这样我们就可以在view文件中方便的用javascript调用modal了。 并且你需要在flash data中设置一个变量,这样你就可以检查表单是否被成功插入的条件。 代码如下,希望对你有帮助:

控制器文件:

    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  ?>

【讨论】:

【参考方案3】:

我得到了答案

控制器

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>

【讨论】:

以上是关于在 php(CodeIgniter) 中不使用 javascript 加载模态的主要内容,如果未能解决你的问题,请参考以下文章

从 url 代码中删除 index.php 在 Godaddy Plesk 托管的 codeigniter 中不起作用?

CodeIgniter &get_instance() 在 php5.6 版本中不起作用?

更新查询在 codeigniter 中不起作用

获取数据在 CodeIgniter 中不起作用

从 URL 中删除 index.php - Codeigniter 2

用逗号分隔的 jQuery 自动完成文本在 CodeIgniter 中不起作用