[英]how can i clear inputs after insert into database with ajax and php?
I would like to clear the inputs after the user submit the form using ajax. 我想在用户使用Ajax提交表单后清除输入。
My code works fine, i made based tutorials and after send fields are still with values. 我的代码工作正常,我编写了基础教程,并且在send字段仍然带有值之后。 I want to clean them. 我要清洁它们。
<script type="text/javascript">
$(document).ready(function(){
$("#inserir").click(function(){
$('#loadingDiv')
.hide()
.ajaxStart(function() {
$(this).show();
})
.ajaxStop(function() {
$(this).hide();
})
;
var nome=$("#nome").val();
var email=$("#email").val();
var confirmacao=$("#confirmacao").val();
var acompanhantes=$("#acompanhantes").val();
// loading image
$("#message").html('<center><img src="images/ajax.gif" /></center>');
$.post('inserir.php', {nome: nome, email: email, confirmacao: confirmacao, acompanhantes: acompanhantes},
function(data){
$("#message").html(data);
$("#message").hide();
$("#message").fadeIn(500);
});
return false;
});
});
</script>
Php p
$nome = $_POST['nome'];
$email = $_POST['email'];
$confirmacao = $_POST['confirmacao'];
$acompanhantes = $_POST['acompanhantes'];
//Insert Data into mysql
$query=mysql_query("INSERT INTO cadastro_rsvp(nome,email,confirmacao,acompanhantes,id) VALUES('$nome','$email','$confirmacao','$acompanhantes','')");
mysql_close($con);
if($query){
echo "Dados enviados";
}
else{ echo "Erro!"; }
}
This will clear all textbox, textarea, radio button, checkbox values: replace " #myFormId " with your form id. 这将清除所有文本框,文本区域,单选按钮,复选框值:用您的表单ID替换“ #myFormId ”。
$('input:text, input:password, input:file, select, textarea', '#myFormId').val('');
$('input:checkbox, input:radio', '#myFormId').removeAttr('checked').removeAttr('selected');
you must add it after this line in your code: 您必须在代码中此行之后添加它:
$("#message").fadeIn(500);
An example using one of your fields: 使用您的字段之一的示例:
$("#nome").val('');
The above code will basically blank whatever is in the textfield. 上面的代码基本上将使文本字段中的内容空白。
我使用HTML来做到这一点。
<button type="reset" value="Reset">Reset</button>
If you want to clear your fields AFTER you save it into the database, you'll have to clear your inputs AFTER you get a response from the server, in this case, in the success
of your $.post
. 如果要在将字段保存到数据库后清除字段,则必须在从服务器获得响应后清除输入,在这种情况下,如果$.post
success
。
$.post('inserir.php',
{nome: nome, email: email, confirmacao: confirmacao, acompanhantes: acompanhantes},
function(data){
$("#message").html(data);
$("#message").hide();
$("#message").fadeIn(500);
//HERE YOU PERFORM THE RESET (I suppose that they are input text fields):
$("#nome").val('');
$("#email").val('');
$("#confirmacao").val('');
$("#acompanhantes").val('');
});
return false;
});
Also, check the jQuery.post documentation for more information to work directly with a success callback function. 另外,请查看jQuery.post文档以获取更多信息,以直接使用成功回调函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.