提交后如何防止页面重新加载?

时间:2016-02-05 07:42:21

标签: javascript php jquery html forms

我正在使用数据库编写PHP编程,我将表单数据提交到同一页面的数据库中,但是当它重新加载时,它会产生令人尊敬的错误。我想使用javascript或jQuery来阻止页面的重新加载,但我对它没有任何想法。

<!DOCTYPE HTML>
<html lang="en">
<head><title>Title</title></head>
<body>
<form method=post>
<label for="name">Name </label>
<input type=text name=name size=30>
<br/>
<input type=submit value=Sent name=submit>
</form>
</body>
</html>

我已经关闭了表格标签,在此我无法做到。 php脚本是

<?php
 $conn=pg_connect("host=hostname dbname=databasename user=username password=password") or die("Error, Could not connect".pg_last_error($conn));
$rs=pg_query($conn,"insert into tablename(name) values($_POST['name']");
if($rs)
{
 echo "Inserted";
 }
else
{
 echo "ERror is occured!";
}
?>

考虑名称在数据库中是唯一的,然后在重新提交表单时生成错误...

It is the second step when I submit the page after fillup the form..

It occurs after the re-submission of the form

请帮我解决问题..                          感谢....

1 个答案:

答案 0 :(得分:0)

您可以使用javascript jquery异步提交表单。您可以使用具有属性'onclick'的按钮来调用您的javascript函数,而不是使用输入类型提交。表单帖子的示例可以是found here

在PHP文件中,您需要返回有用的结果,即状态。从该状态,您可以在收到结果后决定表单的行为,以进入其他页面或显示错误。