Modal loading without using javascript in php (CodeIgniter)

I want to open the model on successful form submission, but although the form does not appear modal, it does not appear.

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>';
    }
}

      

I am trying to open a model in a controller using

   $data = array(
            'first_name' => $fname,
            'last_name' => $lname,
            'email' => $email,
            'password' => $password
        );
        $this->load->model('modal');
        $this->modal->insert($data);

      

+3


source to share


3 answers


I got an answer

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";
        }
     }
}

      



Browse

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

      

0


source


Try to enter the code; it will work



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

      

0


source


Place the modal code in the view file so that we can easily call the modal code with javascript in the view file. And you need to set the variable in the flash data so that you can check the condition that the form was inserted successfully or not. Here is the code, I hope it helps you:

Controller file:

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

      

View file:

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

      

0


source







All Articles