3
I’m making a website that has requests ajax
and login area. The problem is that when I create a form within a modal in bootstrap
for the user to log in, my requests ajax
stop working.
Form code within the modal
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Conteúdo -->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Digite os dados para fazer o login</h4>
</div>
<div class="modal-body">
<form name='form1' class="form-horizontal" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">
<div class="form-group">
<label class="control-label col-sm-2" for="login">Login:</label>
<div class="col-sm-4">
<input type="text" class="form-control" name="login" id="login" placeholder="Digite o login" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="senha">Senha:</label>
<div class="col-sm-4">
<input type="password" class="form-control" name="senha" id="senha" placeholder="Digite a senha" required>
</div>
</div>
<div class=text-right>
<div class="form-group">
<div class="col-sm-4">
<button type="submit" class="btn btn-primary">Acessar</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>
Code that makes the ajax request:
function buscaEndereco(cep)
function buscaEndereco(cep) {
$.ajax({
url: 'php/buscaEndereco.php',
type: 'POST',
async: true,
dataType: 'json',
data:
{
'cep': cep
},
success: function (result) {
if (result != "") {
document.forms[0]["rua"].value = result.logradouro;
document.forms[0]["numero"].value = result.numero;
document.forms[0]["bairro"].value = result.bairro;
}
},
error: function (xhr, status, error) {
alert(status + error + xhr.responseText);
}
});
}
I could not identify what one thing interferes with the other, in case I take the form of the modal, back to work.
When you call the function
buscaEndereco
?– Denis Rudnei de Souza
I didn’t see the function
buscaEndereço
, also want to know if when clicking on Ubmit the data is sent ?– Victor
What appears on the browser console? Is there an error? The network tab shows the request to be made ? Because it has twice the function
function buscaEndereco(cep)
?– Isac
I call the function qnd is being typed the zip code, through the event "onkeyup", yes data is sent, only problem eh that does not fill the fields that must be filled in, as well as find out if I delete the login form it works.
– Bruno Inácio
Do you only have this html on the page? It will not only fill with this information,
document.forms[0]
is theform
login, if you have another form on the page, you will have to call it that:document.forms[1]
– Denis Rudnei de Souza