![](/img/trans.png)
[英]How can i insert the value in database using php, jquery and ajax
[英]how can i clear inputs after insert into database with ajax and php?
我想在用戶使用Ajax提交表單后清除輸入。
我的代碼工作正常,我編寫了基礎教程,並且在send字段仍然帶有值之后。 我要清潔它們。
<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>
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!"; }
}
這將清除所有文本框,文本區域,單選按鈕,復選框值:用您的表單ID替換“ #myFormId ”。
$('input:text, input:password, input:file, select, textarea', '#myFormId').val('');
$('input:checkbox, input:radio', '#myFormId').removeAttr('checked').removeAttr('selected');
您必須在代碼中此行之后添加它:
$("#message").fadeIn(500);
使用您的字段之一的示例:
$("#nome").val('');
上面的代碼基本上將使文本字段中的內容空白。
我使用HTML來做到這一點。
<button type="reset" value="Reset">Reset</button>
如果要在將字段保存到數據庫后清除字段,則必須在從服務器獲得響應后清除輸入,在這種情況下,如果$.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;
});
另外,請查看jQuery.post文檔以獲取更多信息,以直接使用成功回調函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.