使用javascript、ajax和jquery在php中创建表单
我用HTML创建了一个表单,并通过PHP页面处理数据,我希望在同一页面上接收/显示“插入/失败”通知(index.HTML)。我正在使用javascript、ajax和jquery。数据在数据库中更新,但我在process.php页面上得到结果。我想在index.html页面本身插入结果通知 index.html使用javascript、ajax和jquery在php中创建表单,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我用HTML创建了一个表单,并通过PHP页面处理数据,我希望在同一页面上接收/显示“插入/失败”通知(index.HTML)。我正在使用javascript、ajax和jquery。数据在数据库中更新,但我在process.php页面上得到结果。我想在index.html页面本身插入结果通知 index.html <html> <head><title>Registration form</title></head> <body&
<html>
<head><title>Registration form</title></head>
<body>
<form id="myForm" action="process.php" method="POST">
Username: <input type="text" name="username"> <br />
Password: <input type="password" name="pass"> <br />
First name: <input type="text" name="fname"> <br />
Last name: <input type="text" name="lname"> <br />
<button id="submit" name="submit">Register</button>
</form>
<div id="ack">Acknowledge</div>
<script type="text/JavaScript" src="script/jquery-1.11.3.min.js"></script>
<script type="text/JavaScript" src="script/my_script.js"></script>
</body>
</html>
试试这个:
$("#submit").on('click',function(e){
e.preventDefault();
$.post(
$("#myForm").attr("action"),
$("#myForm :input").serializeArray(),
function(info,status,xhr){
$("#ack").empty();
$("#ack").html(info);
clear();
}
);
});
function clear(){
$("#myForm:input").each(function(){
$(this).val("");
});
}
PHP页面(我在PHP页面中重复“success”,因此相同的内容将反映在HTML页面中):
不客气:)只是想知道,为什么它还没有被接受;)粘贴process.php页面的代码。
$("#submit").on('click',function(e){
e.preventDefault();
$.post(
$("#myForm").attr("action"),
$("#myForm :input").serializeArray(),
function(info,status,xhr){
$("#ack").empty();
$("#ack").html(info);
clear();
}
);
});
function clear(){
$("#myForm:input").each(function(){
$(this).val("");
});
}
<?php echo "success";?>
success