I’m trying to open a modal with javascript, if the ajax form is executed, can someone help me?
i am using bootstrap library and jquery
basically if the form is sent so that no error occurs then the modal should open just as the form is sent to the database
index.html
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="js/main.js"></script>
</head>
<body>
<form method="post">
<input type="text" id="nome" name="nome" placeholder="Digite o nome" />
<br/>
<input type="email" id="email" name="email" placeholder="Digite o email" />
<br/>
<input type="button" id="btn_gravar" value="Salvar" name="salvar" />
</form>
<div id="resposta"></div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="js/main.js"></script>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</body>
</html>
here is my javascript form for the request from both backend and front
main.js
$("#btn_gravar").on("click",function(){
var txt_nome = $("#nome").val();
var txt_email = $("#email").val();
console.log(txt_email + " - " + txt_nome);
$.ajax({
url: "create.php",
type: "post",
data:{
nome: txt_nome, email: txt_email
},
beforeSend : function(){
$("#resposta").html("Enviando...");
}
}).done(function(e){
$("#resposta").html("Dados registrado amigo com sucesso...");
$("#exampleModal").modal("show");//<--this line that was to open the modal
})
})
$("#btn_atualizar").on("click",function(){
var txt_nome = $("#nome").val();
var txt_email = $("#email").val();
var txt_id = $("#id").val();
console.log(txt_email + " - " + txt_nome);
$.ajax({
url: "update.php",
type: "post",
data:{
nome: txt_nome, email: txt_email, id: txt_id
},
beforeSend : function(){
$("#resposta").html("Enviando...");
}
}).done(function(e){
$("#resposta").html("Dados atualizado com sucesso...");
window.location="read.php";
})
})
3
Answers
Please move
and
in before
</body>
or you can move it to inside<head>
tagAs Paul Mahardika wrote, move main.js and jquery before the closing body tag. You have to move the bootstrap javascript after jquery, because it uses it.
Like this:
Or just move bootstrap after jquery in
<head>
and wrap your code in main.js in a document.ready function:And you can remove main.js and jquery below
<div id="resposta"></div>
This work for me