11
By sending input fields values via Jquery to a PHP page I can do the action without "refreshing" the main page but I can’t make input fields "empty" again?!? How to apply "reset" after submission?
HTML:
<div id='status'></div>
<form id="new_user" method="post" action="javascript:func()">
<input type='text' name='name' id='name'/>
<br />
<input type='text' name='mail' id='mail'/>
<br /><input type='password' name='password' id='password'/>
<br /><input type='submit' name='submit' id='submit' value='enviar'/>
<br />
</form>
CSS:
#status{
position:absolute;
width: 150px;
height: 30px;
top: 150px;
left:10px;
border: 1px solid black;
color: red;
}
Jquery:
$(function($) {
// Quando o formulário for enviado, essa função é chamada
$("#new_user").submit(function() {
// Colocamos os valores de cada campo em uma váriavel para facilitar a manipulação
var name = $("#name").val();
var mail = $("#mail").val();
var password = $("#password").val();
// Exibe mensagem de carregamento
$("#status").html("<center><img src='core/img/loader.gif' alt='Enviando'/></center>");
// Fazemos a requisão ajax com o arquivo envia.php e enviamos os valores de cada campo através do método POST
$.post('#', {name: name, mail: mail, password: password }, function(resposta) {
// Quando terminada a requisição
// Exibe a div status
$("#status").slideDown();
// Se a resposta é um erro
if (resposta != false) {
// Exibe o erro na div
$("#status").html(resposta);
}
// Se resposta for false, ou seja, não ocorreu nenhum erro
else {
// Exibe mensagem de sucesso
$("#status").html("<center>Cadastro realizado com sucesso!</center>");
// Limpando todos os campos
$("#name").val("");
$("#mail").val("");
$("#password").val("");
}
});
});
});
Thank you friend. Your first example didn’t work with me?!? but the second one was perfect just one note: it stayed inside Isis. Thanks for the help :)
– Lauro Moraes
@Lauromoraes If you open the answer Jsfiddle it is working. You have not forgotten to change the form ID?
– utluiz
I switched yes...but I have no idea what pq didn’t work for me here. The second example (redefining initial values) worked perfectly. After the positive feedback of the data and the response. Again grateful for the help :)
– Lauro Moraes
How do you reset all page fields?
– Dr.G
@Dr.G The second snippet of code shows how to reset a form. If you have multiple Forms on a page, reset them all instead of one. If you need more details, I suggest you ask a new question.
– utluiz