简体   繁体   English

表单仅自动提交一次并重新加载同一页面

[英]Form auto submit only once and reload same page

I have created a form in php which has to fetch data from table1 and dump it into table2 every time user clicks on a link (link provided in a different page).我在 php 中创建了一个表单,每次用户单击链接(链接在不同页面中提供)时,它都必须从table1获取数据并将其转储到table2 I have written code in a php page (assume eg -> test.php) including html as well.我已经在包括 html 的 php 页面(假设例如 -> test.php)中编写了代码。 I tried submitting the form onload of the page using Javascript ( document.formname.submit ) but it keeps on going in an infinite loop and keeps inserting data in table2 again and again.我尝试使用 Javascript ( document.formname.submit ) 提交页面的onload表单,但它一直处于无限循环中,并一次又一次地在table2插入数据。

How do I prevent this and auto form submit only once and still stay on the same PHP (ie test.php) page, which also contains the code for displaying detail view of the inserted data in table2 ?如何防止此表单和自动表单仅提交一次并仍然停留在同一个 PHP(即 test.php)页面上,该页面还包含用于显示table2插入数据的详细视图的代码?

That's because simply calling submit() on a form fill cause a full page reload.那是因为在表单填充上简单地调用submit()会导致整个页面重新加载。 If you want to keep the current approach, you should instead use AJAX call to submit the data from that form.如果您想保留当前的方法,您应该改为使用 AJAX 调用来提交来自该表单的数据。

Read here for more details. 阅读此处了解更多详情。 The article explains how to do both GET and POST requests using JS.文章解释了如何使用 JS 执行 GET 和 POST 请求。

That sad, your solution seems somewhat ... emm ... hairy.可悲的是,您的解决方案似乎有些……嗯……毛茸茸的。 Why exactly are you copying data from no table top another?为什么要从另一个桌面复制数据?

Before JavaScript check this condition by using form name.在 JavaScript 之前,通过使用表单名称检查此条件。

 if ( ! isset($_POST['main']) )

Here main is form name.这里主要是表格名称。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM