I have a small application I am developing to learn AJAX and JavaScript. In this I have method saveData()
that is triggered when a save
button is clicked. All is working fine, but I realized that the page was refreshing so I searched the web and found the JavaScript method event.preventDefault()
. This method worked fine. But that got me into problem, my modal dialog was staying open instead of closing. Again, I found hide
method to "close"
the modal. That solved it, BUT when I click the to open the modal it is bringing me the recent data I sent to the DB. I am from Java background and I began to think that hide
did not kill the modal.
My question: Is there a method to completely destroy the modal the way is done with JFrame.dispose()
method in Java?
The code I was using is below:
function saveData(){
var name=$('#nm').val();
var email=$('#em').val();
var phone=$('#hp').val();
var address=$('#al').val();
event.preventDefault();//prevent the page from refresh
$.ajax({
type:"post",
url:"server.php?p=add",
data:{nm:name,em:email,hp:phone,al:address},
success: function(data){
viewData();
$('#addData').modal('hide');//close the modal.
}
});
}