CodeIgniter Ajax does not populate content in modal form - ajax

The modal form called does not load the content using Ajax. So I have a modal dialog that is supposed to come up with a dropdown and Cancel & OK buttons. The dropdown is loaded from database, however it comes up empty.
I've tried to look this up but examples are either close or very far but none offered something similar.
I've also tried different approaches but still I end up with the same issue.
View:
foreach($aRecords as $record)
...
<a href="#"
class="btn btn-primary btn-xs change_parent_class"
data-toggle="modal"
data-target="#confirmChangeParent"
id ="<?php echo $record->id; ?>"
><i class="fa fa-arrow-up"></i> Change Parent</a>
The modal
<div class="modal fade" id="confirmChangeParent" role="dialog" aria-labelledby="confirmChangeParent" aria-hidden="true">
<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">Change Parent</h4>
</div>
<div class="modal-body">
<p>Please select one of the following options</p>
<div id="areaChangeParent" name="areaChangeParent"></div>
</div>
<div class="modal-footer">
<a href="#" class="btn btn-info btn-xs default" data-dismiss="modal">
<i class="fa fa-reply"></i> Cancel
</a>
<a href="#" class="btn btn-success btn-xs default" id ="confirmSubmitDelete">
<i class="fa fa-arrow-up"></i> Move
</a>
</div>
</div>
</div>
</div>
JavaScript
$(document).ready(function(){
//function change_parent(id)
$(".change_parent_class").click(function()
{
id = $(this).attr('id');
alert("p-aci:" + id);
e.preventDefault();
$.ajax({
url: "<?php echo base_url() ?>project/project_issue/get_parents/",
method: "POST",
data: {mid:id},
success: function(data)
{
$("#areaChangeParent").html(data);
}
});
});
});
Controller function to fetch data is in a dummy state just for testing:
function get_parents()
{
echo "bla";
return "bla bla";
}
No matter what the prompt is displayed but empty.

You are using e.preventDefault(); without function(e)

Related

Modal loading dynamic content not working

I am developing an app which requires options like view,So for viewing details I am using bootstrap modal to show the dynamic details,but the modal is not firing up.
My button
<a href="#" class="btn btn-info btn-sm view" data-id="{{$value->id}}" data-title="{{$value->title}}" data-status="{{$value->status}}">
<i class="fa fa-eye"> Preview</i>
</a>
My modal
<div class="modal fade" id="viewM" tabindex="-1" role="dialog">
<div class="modal-dialog modal-dialog-centered" style="width:60%;">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Preview</h4>
</div>
<div class="modal-body" id="preview" style="padding: 40px;">
</div>
<div class="modal-footer">
<button class="btn btn-danger" type="button" data-dismiss="modal">
<span class="fa fa-times-circle"></span> Close
</button>
</div>
</div>
</div>
</div>
My jquery
$('a.view').click(function()
{
var id = $(this).attr('data-id');
var url = 'receiveReprt';
$.get(url + '/' + id, function (data)
{
console.log(data);
$("#preview").html(data.content);
$('#viewM').modal({show:true});
});
});
Url function
public function receiveReprt($id)
{
$reprt = shareReport::select('content')->where('id','=',$id)->first();
return $reprt;
}
Are you getting any errors in your browser log? My guess at this point is that you are dealing with a javascript error, possibly due to data.content (e.g. if it's not object), which kills the script execution.
If this is the case, and data is not an object as I suspect, you can convert to one (assuming it's JSON of course):
$.get(url + '/' + id, function (data)
{
console.log(data);
$("#preview").html(data.content);
$('#viewM').modal({show:true});
}, "json");
Or
$.ajax({
url: url + '/' + id,
method: "get",
success: function (data) {
var json = $.parseJSON(data);
$("#preview").html(json.content);
$('#viewM').modal({show:true});
}
});

Using class name to trap .click() event

My ajax code is not catching the .click event with following type of code wherein I trying to trap the .click event using view_data.
<div id="dataModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Need to Pud ID and Name of Work here..What the....</h4>
</div>
<div class="modal-body" id="workstatustable">
<input type="button" name="AMWP/2017-18/1" value="AMWP/2017-18/1" id="AMWP/HQTC/2017-18/1" class="btn btn-info btn-xs view_data">
<input type="button" name="AMWP/2017-18/2" value="AMWP/2017-18/2" id="AMWP/HQTC/2017-18/2" class="btn btn-info btn-xs view_data">
<input type="button" name="AMWP/2017-18/3" value="AMWP/2017-18/3" id="AMWP/HQTC/2017-18/3" class="btn btn-info btn-xs view_data">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info btn-sm" data-dismiss="modal">Update Status</button>
<button type="button" class="btn btn-info btn-sm" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
my ajax code is as follows:-
$('.view_data').click(function(){
var modwork_id = $(this).attr("id");
$('#show_modid').html($(this).attr("id"));
fetchModal();
});
function fetchModal() {
var modwork_id = $("#show_modid").html();
window.alert($("#show_modid").html());
$('#viewstatus_Modal').modal("show");
$.ajax({
url:"workstatus.php",
method:"POST",
data:{modwork_id:modwork_id},
success:function(data){
$('#workstatustable').html(data);
$('#viewstatus_Modal').modal("show");
}
});
};
The issue is the modal window is not popping up.
the problem is in here.
you didn't declare the class properly as u write in ajax is .view_data and #show_modid
you have to do this >>
class="modal view_data fade "
so you can use ".view_data" in your ajax.
and do this >>
"#dataModal"
in your ajax
hope this will help you.
I got the soulution to the problem. I was trying to attach a function() to HTML item before the item/element was declared or found on the page. I inserted the ajax() call in the Success: clause. The plan is now working. Thanks.
Got It... I have relocated the position of the ajax() and its cool....
function fetchModal() {
var modwork_id = $("#show_modid").html();
window.alert($("#show_modid").html());
$('#viewstatus_Modal').modal("show");
$.ajax({
url:"workstatus.php",
method:"POST",
data:{modwork_id:modwork_id},
success:function(data){
$('#workstatustable').html(data);
$('#viewstatus_Modal').modal("show");
$('.view_data').click(function(){
var modwork_id = $(this).attr("id");
$('#show_modid').html($(this).attr("id"));
fetchModal();
});
}
});
};

Delete data using modal box in laravel

I have some problem to delete data with confirmation (in this case using modal box) in laravel.
This is my delete button
{{ Form::open(array(
'route' => array('delete_spk', $spk_data->id),
'method' => 'put',
'style' => 'display:inline'
))
}}
<button class="btn btn-danger btn-line btn-rect" type="submit" data-toggle="modal" data-target="#delSpk" data-title="Delete SPK" data-message='Are you sure you want to delete this data ?'>
<i class="icon-trash icon-white"></i> Delete</button>
{{ Form::close() }}
This is the modal box
<!--MODAL DELETE SPK-->
<div class="col-lg-12">
<div class="modal fade" id="delSpk" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<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="H4"> Delete SPK</h4>
</div>
<div class="modal-body">
<p class="help-block">Are you sure you want to delete this data ?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger btn-line btn-rect" id="confirm">Yes</button>
<button type="button" class="btn btn-primary btn-line btn-rect" data-dismiss="modal">No</button>
</div>
</div>
</div>
</div>
</div>
<!--END OF MODAL DELETE SPK-->
<!-- Dialog show event handler -->
<script type="text/javascript">
$('#delSpk').on('show.bs.modal', function (e) {
$message = $(e.relatedTarget).attr('data-message');
$(this).find('.modal-body p').text($message);
$title = $(e.relatedTarget).attr('data-title');
$(this).find('.modal-title').text($title);
// Pass form reference to modal for submission on yes/ok
var form = $(e.relatedTarget).closest('form');
$(this).find('.modal-footer #confirm').data('form', form);
});
<!-- Form confirm (yes/ok) handler, submits form -->
$('#delSpk').find('.modal-footer #confirm').on('click', function(){
$(this).data('form').submit();
});
</script>
This is the route
Route::get('spk/destroy/{id}', array('as'=>'delete_spk','uses'=>'SpkController#destroy'));
And this is the Controller for delete the data
public function destroy()
{
$spk= Spk::find(Input::get('id'))->delete();
Session::flash('message', 'Successfully deleted the SPK !');
return Redirect::to('spk_view');
}
The modal box is working, but when I'm getting the ID to delete, this will be ended with results "method not allowed http exception". Can someone help me please?
This issue happens because you have defined the route as GET but submitting the form as PUT
If you define you route as below then it must work
Route::put('spk/destroy/{id}', array('as'=>'delete_spk','uses'=>'SpkController#destroy'));

Bootstrap 3.1 with laravel 4.0

I have problem to display confirmation dialog box when I click the delete button it show the confirmation dialog and also submit the form without clicking the button in confirmation dialog box.
I'm using bootstrap and laravel. here is the code.
<div class="modal fade" id="confirmDelete" role="dialog" aria-labelledby="confirmDeleteLabel" aria-hidden="true">
<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">Delete Confirmation</h4>
</div>
<div class="modal-body">
<p>Are you sure you want to delete this data(s) ?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-danger" id="confirm">Delete</button>
</div>
</div>
</div>
$('#confirmDelete').on('show.bs.modal', function (e) {
// Pass form reference to modal for submission on yes/ok
var form = $(e.relatedTarget).closest('form');
$(this).find('.modal-footer #confirm').data('form', form);
});
<!-- Form confirm (yes/ok) handler, submits form -->
$('#confirmDelete').find('.modal-footer #confirm').on('click', function(){
$(this).data('form').submit();
});
{{ Form::open(array('route' => array('subscription.delete.all'), 'method' => 'delete')) }}
<button class='btn btn-xs btn-danger' type='submit' data-toggle="modal" data-target="#confirmDelete">
<i class='glyphicon glyphicon-trash'></i> Delete
</button>
{{ Form::close() }}
on top of your modal add this
<a href="#" type="button" class="btn btn-default btn-danger" data-toggle="modal" data-target="#confirmDelete">
delete
and in your modal code change the button
<button type="button" class="btn btn-danger" id="confirm">Delete</button>
by your delete button code
{{ Form::open(array('route' => array('subscription.delete.all')......
Change button (inside the form) type from "submit" to "button"

Is it possible to call a modal using ajax?

$.ajax({
type : 'POST',
data : "",
url : '<?php echo site_url("adduser/register_user");?>',
success : function(data){
$('#error').modal('show');
}
});
the codes above is my code to show a modal but it didn't work. Is it possible to show a modal using ajax?
It seems you use Bootstrap right?
If you read the documentation there is a better way to achieve the same result:
In your main file:
<a data-toggle="modal" href="<?php echo site_url('adduser/register_user');?>" data-target="#myModal">Click me</a>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"></div>
And then in the remote file:
<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">Modal title</h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
This will show the default modal. More information in the manual: Bootstrap Modal

Resources