如何执行“HTML FORM action = location.replace('nextpage.html')”? [英] How to do "HTML FORM action=location.replace('nextpage.html')"?
问题描述
1.转到main.html中的页面(比如说,转到page1.html)。
2.在第1页的表单中收集数据。 >
3.在提交时,转到page2.html(使用PHP将数据保存到数据库并公布结果)。
4.单击确定并返回主菜单页面。
似乎合理吧?
上面的第4步是我的问题。
我试过的东西:
1.调用history.back在第2页点击Ok。
2.在第2页的确定中调用javascript location.href = main.html
这适用于并转至main但点击主菜单中的返回按钮会将用户带回第1页。 (不好)
3.在page1的action =和onSubmit()中使用javascript history.replace()的许多变体。
并且从窗体输入不是在查询字符串中传递给page2 )
4.清除历史记录
由于安全考虑,不可能是不可能的。
5.以编程方式为第1页上的history.replace(从表单输入)组成查询字符串。
是非常费力,容易出错和可怕的不雅
任何建议将不胜感激。
========= MY SOLUTION 4/8/16 ==============
我将下面的最终解决方案发布给任何有兴趣的人。
在page1.html中:
< script type =text / javascript>
function addarg(q,addend)
{
qnew = q + addend;
return(qnew);
函数Next()
{
//从FORM $ b获取输入值b b frm = document.forms [MainForm];
first = frm.first.value;
last = frm.last.value;
phone = frm.phone.value;
//合成查询
query =processForm.php;
query = addarg(query,'?first ='+ first);
query = addarg(query,'& last ='+ last);
query = addarg(query,'& phone ='+ phone);
location.replace(query); //链接到处理页面
}
< / script>
...
< / form>
当从processForm.php发出history.back()时,它会根据需要返回主菜单。
看起来更简单的方法是将简单的< a href = main .html>确定< / a>
在 page2.html 上。
原则是:如果 page1.html 由后退调用,则必须是清除 pa ge1.html ,没有显示任何数据输入用户最终输入。
The title is actually a failed attempt to achieve my goal. Specifically, I want to
1. Go to a page from main.html (say, to page1.html).
2. Collect data in a form on page1.
3. On Submit, go to page2.html (which saves data to database using PHP and announces result).
4. Click on "Ok" and go back to main menu page.
Seems reasonable, right?
Step 4 above is my problem.
Things I've tried:
1. Invoke history.back on Ok click in page2.
This goes to page1 not main
2. Invoke javascript location.href=main.html on "Ok" in page2
This works and goes to main but clicking on Back button in main brings user back to page1. (not good)
3. Many variations of using javascript history.replace() in action= and onSubmit() of page1.
This works works and but inputs from form are not passed in query string to page2)
4. Clear history.
This appears to be impossible because of security concerns.
5. Programmatically composing the query string for history.replace (from form inputs) on page1.
This works but is very laborious, error-prone and horribly inelegant
Any suggestions will be greatly appreciated.
========= MY SOLUTION 4/8/16 ==============
I'm posting my final solution below for anybody that's interested.
In page1.html:
<script type="text/javascript">
function addarg(q,addend)
{
qnew = q + addend;
return( qnew );
}
function Next()
{
// GET INPUT VALUES FROM FORM
frm = document.forms["MainForm"];
first = frm.first.value;
last = frm.last.value;
phone = frm.phone.value;
// COMPOSE QUERY
query = "processForm.php";
query = addarg( query, '?first='+first );
query = addarg( query, '&last='+last );
query = addarg( query, '&phone='+phone );
location.replace( query ); // chain to processing page
}
</script>
<form action="javascript:Next()" name="MainForm" method=GET>
...
</form>
When history.back() is issued from processForm.php, it returns to Main Menu as desired.
It seems the easier way to achieve this is to place a simple <a href=main.html>Ok</a>
on your page2.html.
Then associate a flag (maybe a cookie) on this main.html which "instructs" page1.html to clear its form elements if it is not called by main.html, but from a "back" button (by refreshing meta, for example, or by loading page1.html#randomnum, where randomnum is a value you find real time to force your browser understand this is not the "old" page1.html but a "new" page1.html, helping browser not to get this from cache but from web).
The principle is: if page1.html is called by "back", it have to be a "clear" page1.html, not showing any data input user eventually entered.
这篇关于如何执行“HTML FORM action = location.replace('nextpage.html')”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!