[英]How to remain on the same page after clicking submit on a form?
I have an html file/page where there will be multiple input fields in a form. 我有一个html文件/页面,表单中会有多个输入字段。 This form action field is linked to another .php file where all the inputted data is written to a database. 此表单操作字段链接到另一个.php文件,其中所有输入的数据都写入数据库。 But once when i click submit the form i get directed to the .php file which is not what i want.Is there any way by which i could write the data into the database ,but remain in the same html file? 但是,当我点击提交表单时,我会被定向到.php文件,这不是我想要的。有什么方法可以将数据写入数据库,但保留在同一个html文件中?
If you want to do this, you have to use javascript and ajax. 如果你想这样做,你必须使用javascript和ajax。
your form: 你的表格:
<form id="yourform">...</form>
javascript: JavaScript的:
$(function() {
$('#yourform').on('submit', function(e) {
var data = $("#yourform :input").serialize();
$.ajax({
type: "POST",
url: "script.php",
data: data,
});
e.preventDefault();
});
});
of course. 当然。 use ajax. 使用ajax。 do some web searches on ajax combined with html and you will find the answers you are looking for. 在ajax上结合html进行一些网络搜索,你会找到你正在寻找的答案。 jquery is one possible javascript/ajax platform that could simplify this process for you. jquery是一个可能的javascript / ajax平台,可以为您简化此过程。
You have 2 options: 你有2个选择:
1 - Submit you form using AJAX. 1 - 使用AJAX提交表单。 With this approach the page will stay "refresh" and the user will be on the same page 使用这种方法,页面将保持“刷新”,用户将在同一页面上
2 - After the submit, redirect the user back to the previous page. 2 - 提交后,将用户重定向回上一页。
You could make the submit button send the form data to a div tag on the same page using ajax. 您可以使用ajax使提交按钮将表单数据发送到同一页面上的div标签。 The code from Dynamic Drive should work if you read through it. 如果您仔细阅读,动态驱动器中的代码应该可以使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.