简体   繁体   English

单击表单上的提交后如何保持在同一页面上?

[英]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. 如果您仔细阅读,动态驱动器中的代码应该可以使用。

Dynamic Drive - Dynamic Ajax Content 动态驱动 - 动态Ajax内容

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在表单提交到数据库之后实现模式窗口并保持在同一页面上 - Implementing modal window after form submit to database and remain on the same page 表单提交后,如何防止页面刷新并使它保留在同一选项卡部分中 - After form submit how to prevent page refresh and make it to remain in same tab section 单击联系表单上的“提交”后,如何在wordpress中停留在同一页面上? - after clicking submit on a contact form, how do i stay on the same page in wordpress? 单击提交后如何重定向到同一“联系我们”页面 - how to redirect to the same contact us page after clicking submit 用户点击提交后,页面跳转到php文件。 它如何保留在 HTML 文件中并将表单信息提交到电子邮件? - After the user clicks submit, the page goes to the php file. How can it remain on the HTML file and submit the form information to the email? 使用href提交表单后,如何在同一页面上获取文本? - How to get text on same page after form submit using href? 如何在表单提交后在同一个html页面上回显? - How to echo on same html page after form submit? 需要后如何提交表单并保持在同一页面上? - How can I submit form and stay on the same page after require? 如何在同一页面上提交表单后显示消息 - How to display message after form submit on same page 如何在不单击提交按钮的情况下提交页面加载表单? - How to submit a form on page load without clicking submit button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM