Laravel show subview with condition after ajax success - laravel

I´m in a point where i need to show a success(or not) message, i like modals because they freeze everything behind(the main view) and show that little box with some kind of message. I have seen many examples, but most of them are showing these "alert" boxes per page and i think i should have a view(called modal) and every time i need to show it, i just pass the proper arguments and the view would show up.
What i have done:
Created a view called modal.
Created a controller with a method that will receive the arguments.
Created a route to the controller.
$.ajax({
method: 'POST',
url: '/angariacoes/insertImovel',
dataType: "json",
data: {"_token": "{{ csrf_token()
}}",values,eachImage,eachImageThumb},
success: function(response){
window.location.href='/popup/popup';
},
error: function(jqXHR, textStatus, errorThrown) {
}
});
But now, how do i show that view(that has boostrap modal code) as a modal? Anyone?
Thanks, regards

Put a div inside of the modal body and then you can just pass back a view as your response:
$.ajax({
method: 'POST',
url: '/angariacoes/insertImovel',
dataType: "json",
data: {"_token": "{{ csrf_token()
}}",values,eachImage,eachImageThumb},
success: function(response){
$('#myAjaxTarget').html(response);
$('#myModal').modal('show');
},
error: function(jqXHR, textStatus, errorThrown) {
}
});
Controller:
return view('my_view', compact($data));
Just make sure the actual modal part is on the view that you want the pop up to be. Create a separate blade that has the modal content.
So I have the_main_view.blade.php, and in it I have put:
<div class="modal fade" id="myModal" tabindex="-1" role="img" style="overflow: hidden">
<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="">Modal Title</h4>
</div>
<div class="modal-body" id="myAjaxTarget">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
And we have a different view, my_view.blade.php that is being returned via the Controller method mentioned above:
<span>Literally any HTML</span>
So the response from the ajax call is just my_view, which you use to replace the HTML content of #myAjaxTarget with. So the end modal will look like this:
<div class="modal fade" id="myModal" tabindex="-1" role="img" style="overflow: hidden">
<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="">Modal Title</h4>
</div>
<div class="modal-body" id="myAjaxTarget">
<span>Literally Anything</span>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Which is then shown with the $('#myModal').modal('show') method.

Related

on submit doesn't capture event inside modal

I have and inside it I have the form tag with its id, since the form is large I put it on another file and call it with php
<div class="modal fade" id="general" tabindex="-1" role="dialog" aria-labelledby="generalLabel" aria-hidden="true">
<div class="modal-dialog modal-xl" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="generalLabel">Historia clinica general</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form method="POST" role="form" id="hcgeneral">
<?php require 'addHcg.php' ?>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cerrar</button>
<button type="submit" class="btn btn-primary">Guardar</button>
</div>
</div>
</div>
</div>
and then there's the jquery code, where I have the ajax request, before it I have the submit event with the id of the form but it doesn't capture it and I don't know why because I already did something exactly like this
$('#hcgeneral').submit(function (e) {
e.preventDefault()
$.ajax({
type: 'POST',
url: 'procHcg.php',
dataType: 'JSON',
data: {
id_paciente: $('input[id="id_paciente"]').val(),
embarazo: $('.embarazo').val(),
padreConvida: $('.padreVivo').val(),
enfermedadPadre: $('#enfPadre').val(),
madreConVida: $('.madreViva').val(),
enfermedadMadre: $('#enfMadre').val(),
hermanos: $('.hnos').val(),
hnoSano: $('#hnoSano').val(),
sufreEnfermedad: $('.sufreEnf').val(),
queEnfSufre: $('#pacEnf').val(),
tratMedico: $('.haceTrat').val(),
cualTratMedico: $('#tratMedico').val(),
medHabituales: $('#medConsume').val(),
med5anios: $('#medicamentos5').val(),
deporte: $('.deporte').val(),
malestarDeporte: $('.malestar').val(),
extraccionMuela: $('#sacaMuela').val(),
sangraMuela: $('#sangraMuela').val(),
colageno: $('.probColageno').val(),
fiebre: $('.fiebre').val(),
fiebreProtege: $('#protegeMed').val(),
diabetico: $('.diabetico').val(),
diabetesControl: $('.diabetesControl').val(),
diabetesMed: $('#diabetesMed').val(),
//and more data...
},
success: function (response) {
console.log(response)
alert('La historia clinica general se ha guardado exitosamente.')
},
error: function (jqXMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
alert(errorThrown)
}
})
What I want is to see the postDatos in the console and to sent them in procHcg.php but nothing returns

CodeIgniter Ajax does not populate content in modal form

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)

Bootstrap modal not close after submit data through jquery ajax method

i wanna put data into database through bootstrap modal with ajax. But after submit data button on modal the modal not close automatically. How i can fix this.
Here is code of Bootstrap modal
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
This is ajax code
<script type="text/javascript">
***
SOme Code Here
****
$.ajax({
type: 'GET',
url : "YOu URL",
data: {},
success: function(feedback){
$('#feedback').html(feedback);
}
});
});
});
</script>
In your success method, you call to close the model by using
$('#yourModal').modal('hide');
Btw, it is advisable to do a http POST rather than a GET for submissions.
The syntax for modal closing seems fine. But I'm not sure if you are targetting right modal element. See if the target id on which you are triggering the modal close code is right. Hope it solves your problem.

bootstrap 3 modal breaking on AJAX response - mvc4

My bootstrap modal footer just doesn't show up.
Main View
<div class="modal fade" id="ModalId">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" data-dismiss="modal">Log</button>
</div>
<div class="modal-body">
<div id="form">
<div id="ModalBodyId">
#Html.Partial("PartialViewName");
</div>
</div>
</div>
<div class="modal-footer"> // this part doesn't show up
<button type="button" data-dismiss="modal" onclick="SaveNow()";>Edit</button>
</div>
</div>
</div>
</div>
Here is what loads the partial view into the modal. I had to add the below code for the validation to work. The validation works fine now, however i do not see the button which is contained inside the modal footer.
$("#ModalId").click(function () {
var form = $("#ModalBodyId").closest("form");
form.removeData('validator');
form.removeData('unobtrusiveValidation');
$.validator.unobtrusive.parse(form);
$.ajax({
url: "/ControllerName/ActionName",
type: "GET",
data: $("#ModalBodyId").serialize(),
cache: false
});
});
If i remove the #Html.Partial("PartialViewName"); from modal-body then the modal-footer shows up.

How to popup modal only when the Ajax function finish execution in bootstrap jquery?

I have a Ajax function like this:
function generate_summary() {
var summary_input=document.getElementById('summary_input').value;
var email_notify=document.getElementById('email_notify').value;
var num_sent=document.getElementById('num_sent').value;
$.ajax({
type: "GET",
url: "/summary/",
data: {summary_input:summary_input, email_notify:email_notify, num_sent:num_sent},
success: function(data){
$('#modal-body').html(data)
}
});
}
I have href like this:
<a onclick="generate_summary()" href="#summaryModal" style="text-decoration:none;" id="summarize_btn" role="button" class="btn btn-success offset3" data-toggle="modal">Summarize</a>
Modal looks like this:
<!-- Summary output popup -->
<div id="summaryModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Summary</h3>
</div>
<div class="modal-body" id="modal-body">
<textarea disabled id="summary_output" name="summary_input" rows="15" class="span5"></textarea>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button class="btn btn-primary">Save changes</button>
</div>
</div>
Modal is a popup when summarize button is clicked.
The output from the server is placed on:
<div class="modal-body" id="modal-body">
<textarea disabled id="summary_output" name="summary_input" rows="15" class="span5">{{ summary }}</textarea>
</div>
Ajax function, modal everything is working fine. But the modal is load so quickly and the function only returns the value after 10-15 secs. I want to show the popup modal and the summary at the same time. How can I do that?
to use jquery dialog you will have to reference jquery.ui js and css files in your project.
first make the summaryModal a jquery dialog:
$("#summaryModal").dialog({ autoOpen: false, buttons:
{
"Save": function () {
if (!confirm("Do you want to save the changes?")) {
return;
}
//Add your logic to save.
$("#summaryModal").dialog('close');
},
"Cancel": function () {
$("#summaryModal").dialog('close');
}
}
});
After that when you click the summarize_btn make the ajax call and load the content in the dialog:
$("#summarize_btn").click(function () {
$.ajax({
type: "GET",
url: "/Home/About",
success: function (data) {
$("#summaryModal").dialog("option", "height", 500);
$("#summaryModal").dialog("option", "width", 500);
$("#summaryModal").dialog("option", "title", 'Modal Title');
$("#summaryModal").dialog("option", "modal", true);
$("#summaryModal").dialog('open');
$('#summaryModal').html(data);
}
});
});
the summaryModal div will look like:
<div id="summaryModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
</div>

Resources