2016-11-04 30 views
-1

我有一個表單需要從表單發佈數據並運行腳本以基於表單中的用戶輸入重定向頁面。表單按鈕發佈數據並通過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的形式將數據發送到我的郵箱,但形式不發送。該網站是: carbonator

我可以簡單地將post()添加到腳本中嗎?我在這裏研究了這些,發現了一些非常混亂的解決方案,這些解決方案有點相同,但並不完全相同。

回答

1

爲什麼你不使用jQuery?

如果你使用jQuery,你可以簡單地使用$.post()函數。

+1

我不知道。我在哪裏可以使用$ .post()? – Arnolfo

+0

$ .post(「myfile.php」,{argName:「argValue」,arg2Name:「arg2Value」}); 更多的信息在這裏:https://api.jquery.com/jquery.post/ – creekorful

+0

這是假設我知道jquery,我不知道。無論如何只要添加代碼,我必須發佈併發送電子郵件? – Arnolfo

0

我無法獲得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"); 

這有希望運作良好。

相關問題