【问题标题】:Submit form and change page提交表格和更改页面
【发布时间】:2013-12-18 21:05:03
【问题描述】:

我正在尝试提交表单,打开一个接收数据的新页面,而当前页面将返回索引。

这是我尝试做的:

<form method="post" action="newpage.php" target="_blank" onsubmit="location.replace('index.php')">
//inputs
<input type=image src="image.jpg" alt="Send" value=submit />
</form>

所以我想要它做的是在新选项卡中打开 newpage.php,并在当前选项卡中打开 index.php,但它似乎并没有那样工作。 我也尝试添加

onclick="location.replace('index.php')

到我的提交输入,但它也不起作用。

有没有办法做到这一点? 谢谢

【问题讨论】:

  • 使用 Ajax 可能会容易得多。
  • 我从来没有用过Ajax,有没有简单的方法适合初学者?
  • 看看比利在下面发布的内容。看起来不错。

标签: javascript php html forms submit


【解决方案1】:
//If you want to use Ajax + jQuery:

var fromData = $(#formID).serialize(); // your form's data
$.ajax({
  type: "POST",
  url: "newpage.php",
  data: fromData //sends the data to the new page.
})
  .done(function( msg ) {
    window.location.href = 'index.php' // redirects the page when finished.
});

【讨论】:

  • 正如我在另一条评论中所说,我以前从未使用过 Ajax。我应该在单击按钮时调用的函数中的
  • 是的,我编写的代码需要在
【解决方案2】:

简答:你应该把重定向放在newpage.php(到index.php

解释:onsubmit 事件在点击提交按钮时触发,在页面卸载之前,之后动作页面被加载。注意:如果onsubmit返回false,则取消提交。

因为您的页面已卸载,它显然无法强制进行任何重定向。您应该从新加载的操作页面重定向。

http://www.w3schools.com/jsref/event_form_onsubmit.asp

【讨论】:

    猜你喜欢
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2014-03-09
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 2020-06-14
    相关资源
    最近更新 更多