on submit doesn't capture event inside modal - ajax

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

Related

Bootstrap 4 destroy a modal when it is closed

I'm looking for a solution on my problem. I'm using bootstrap4 on laravel project. I have a modal that has a button which close it self and open a new modal. This is the code:
$("#newAddressBtn").on('click', function () {
$('#modalAddressees').modal('hide')
$('#modalAddressees').on('hidden.bs.modal', function () {
// Load up a new modal...
$('#modalNewAddress').modal('show');
})
});
Inside this new modal (modalNewAddress) I have a button that dismiss it. But when I dismiss it, I would to destroy it self because if I need to re-open it again, I would to start with empty data inside (such us the first show).
I wrote this code but nothing happens:
[....]
.on('core.form.valid', function () {
$.ajax({
url: "{{ route("addresses.store") }}",
headers: {'X-CSRF-Token': $('meta[name=_token]').attr('content')},
dataType: "JSON",
data: $("#addressForm").serialize(),
type: "POST",
success: function (data) {
destroyModal("#modalNewAddress");
},
error: function (data) {
console.log('error ' + data);
}
});
});
[....]
function destroyModal(modal){
$(modal).modal('dispose');
}
This is the code of the modal:
<div class="modal fade" tabindex="-1" role="dialog" id="modalNewAddress">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">New Address</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" id="addressForm" action="{{ route("addresses.store") }}" class="needs-validation" novalidate="">
{{ csrf_field() }}
<div class="form-group row">
<label for="name" class="col-sm-3 col-form-label">Name</label>
<div class="col-sm-9">
<input type="text" class="form-control" name="name" id="name" value="" placeholder="Name" required="">
</div>
</div>
<span class="float-right">
<button type="submit" id="save" class="btn btn-primary">Save</button>
</span>
<input type="hidden" name="ajax">
</form>
</div>
<div class="modal-footer bg-whitesmoke br">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
How can I solve the problem? Could you please help me?
You're not supposed to destroy the modal but recycle it.
The usage pattern would be as follows:
the modal does not contain data, but data fields you can reach with jQuery, such as:
<div id="adress-nome"></div>
<!-- more fields ... -->
before you open the modal, you populate it with a function, such as:
function updateModal(data)
{
$("#address-nome").val(data.nome);
// more data ...
}
function openModal(data)
{
updateModal(data);
$("#address-modal").modal("show");
}
when you're done with the modal, just hide it
when you re-open it, it will contain the new data

Laravel show subview with condition after ajax success

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.

How handle success load of data with bootstrap modal?

We have:
$('#myModal').modal({
show: true,
remote: '/some/api/url/',
//onComplete: function() ???
//success: function() ???
});
How for example make console.log() every time when data successfuly loaded?
From documentation:
remote
This option is deprecated since v3.3.0 and has been removed in v4. We recommend instead using client-side templating or a data binding framework, or calling jQuery.load yourself.
In any case, you can use:
loaded.bs.modal This event is fired when the modal has loaded content using the remote option
So the code, in your case will be:
$('#myModal').on('loaded.bs.modal', function (e) {
// do something...
})
An example:
$('#myModal').modal({
show: true,
remote: 'https://api.github.com/users/defunkt'
});
$('#myModal').on('loaded.bs.modal', function (e) {
$("#myModal").css({"height":'150px',"overflow-y":"auto"});
console.log('remote content loaded');
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></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>
</div>
</div>

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

Typo3 6.x extension Cache issue with extbase eID ajax data passed to modal

The data from ajax (json) is fine and up to date, but the data shown in my modal is the one from the first page load. The data in "console.log" ist correct. Seems to be a chache issue?
$('.showCallhistory').click(function(e) {
var callsId = $(this).data("id");
$.ajax({
cache: false,
url : '?eID=ajaxDispatcher&callId='+callsId,
dataType : 'json',
success : function(data) {
var callhistoryhtml = '';
callhistoryhtml +='<div class="well"><dl class="dl-horizontal">';
$.each(data, function(i, val) {
callhistoryhtml += '<dt>'+val.chCrdate+'</dt><dd>'+val.chEntry+' | durch: '+val.feUsername+'</dd>';
console.log(val.chEntry + ' : ' + val.chCrdate+ ' : ' + val.feUsername);
});
callhistoryhtml += '</dl></div>';
$('.replaceMe').replaceWith(callhistoryhtml);
$('#myModal').modal('toggle');
},
error : function(result) {
alert('error ' + result.myResult);
},
});
});
my modal, in which the content is not properly replaced:
<div class="modal fade" id="myModal" 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="myModalLabel">Call-Historie</h4>
</div>
<div class="modal-body replaceMe">...</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">schliessen</button>
</div>
</div>
</div>
</div>
The problem was the:
$('.replaceMe').replaceWith(callhistoryhtml);
Now using following instead and this brings the needed effect:
$('.replaceMe').empty();
$('.replaceMe').append(callhistoryhtml);
Are there any comments/better ways?

Resources