问题
I want to open model on successful submission of form but although form is submitting modal is not popped up.
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">×</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 the model in controller using
$data = array(
'first_name' => $fname,
'last_name' => $lname,
'email' => $email,
'password' => $password
);
$this->load->model('modal');
$this->modal->insert($data);
回答1:
Try below code; this 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">×</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:
Put the code of modal in a view file so that we can easily call the modal with javascript in the view file. And you need to set a variable in flash data so that you can check the condition that the form has been succesfully inserted 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">×</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:
I got the 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";
}
}
}
VIEW
<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">×</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>
来源:https://stackoverflow.com/questions/44692125/modal-not-loading-using-javascript-in-phpcodeigniter