表单按钮张贴数据和运行脚本通过onclick


form button to post data and run script via onclick

我有一个表单,既需要从表单发布数据,又需要运行一个脚本,根据用户从表单输入的内容重定向页面。

我已经成功地运行了脚本,但无法找到一种方法来发布从表单的数据。提交按钮必须是type="button",否则jscript onclick="calc()"将无法运行。我不知道为什么。

我的按钮代码是:

<input type="button"  id="calculate" value="Calculate"  onclick="calc()" formaction="carbondata.php" formmethod="post"/>

和我的脚本是

<script type="text/javascript">
  function calc() {
    if (document.getElementById('mileage').value == '0-15000' && document.getElementById('gdi').value == 'yes' && document.getElementById('fuel').value == 'yes') {
      window.location = 'http://www.arnolfodesign.com/clients/itw_carbonator/outcome01.html'
    } else if (document.getElementById('mileage').value == '0-15000' && document.getElementById('gdi').value == 'yes' && document.getElementById('fuel').value == 'no') {
      window.location = 'http://www.arnolfodesign.com/clients/itw_carbonator/outcome02.html'
    }
  }
</script>

我有一个简单的php表单发送数据到我的电子邮件,但表单不发送。网址是:碳酸化器

我可以简单地将post()添加到脚本中吗?我在这里研究了这个问题,发现了一些非常令人困惑的解决方案,这些解决方案有些相同,但并不完全相同。

为什么不用jQuery呢?

如果你使用jQuery,你可以简单地使用$.post()函数

我无法获得jquery .post()函数来发布和重定向用户。但是,我能够在post.php页面中完成这两件事。

添加了php重定向。

 if ($mileage == "0-15000" && $gdi == "yes" && $fuel == "yes")  {
header("Location:http://www.arnolfodesign.com/clients/itw_carbonator/outcome01.html");
 } elseif ($mileage == "0-15000" && $gdi == "yes" && $fuel == "no") {
  header("Location:http://www.arnolfodesign.com/clients/itw_carbonator/outcome02.html");
 } elseif ($mileage == "0-15000" && $gdi == "no" && $fuel == "yes") {
 header("Location:http://www.arnolfodesign.com/clients/itw_carbonator/outcome01.html");
 } elseif ($mileage == "0-15000" && $gdi == "no" && $fuel == "no") {
header("Location:http://www.arnolfodesign.com/clients/itw_carbonator/outcome02.html");

希望这也能正常工作。