我是javascript的新手,我有一个带有多个提交功能的表单,使用javascript和php刷新。每次刷新页面时都可以查看警报,而不是转移到新页面 我的Java脚本:
<script>
function submitFunction(i)
{
if(i==1){
alert("Transferring to page1.php");
document.flight_select.action="page1.php";
document.flight_select.submit();
}
if(i==2){
alert("Transferring to searchflights.php");
document.flight_select.action="search_flights.php";
document.flight_select.submit();
}
}
</script>
这是我的PHP代码:
echo "
<form id=\"flight_select\" method=\"POST\">; //Some formatting code follows this for displaying table;
echo "<tr><td colspan=\"2\" >
<input type=\"submit\" value = \"< New Search\" onClick=\"submitFunction(2)\";/></td>
<td colspan=\"3\"><center>"."<input type=\"submit\" value=\"Make Booking for selected flight >\" onClick=\"submitFunction(1)\"/>"."</center>
</td></tr>";
答案 0 :(得分:0)
您没有取消点击会触发的原始提交。使用return false
是取消默认点击事件处理程序的一种方法,因此在提交之前它不会提交您的表单。在您的代码中,最简单的方法是onClick="submitFunction(2);return false"
。
此外,请考虑不要执行onClick="javascriptcode"
,而是从JavaScript端附加处理程序,也不要使用echo
输出HTML,以保持代码和表示的分离。
答案 1 :(得分:0)
As per your suggestion, I modified the code to look something like this and that resolved my issue:
<script>
function submitFunction(i)
{
if(i==1){
alert("Page1.php");
document.getElementById("flight_select").action="page1.php"
document.getElementById("flight_select").submit();
}
if(i==2){
alert("searchflights.php");
document.getElementById("flight_select").action="search_flights.php"
document.getElementById("flight_select").submit();
}
}
</script>
<html>
<head><title>Multiple submit test</title></head>
<body>
<form id="flight_select" method="POST">
First Name: <input type="text" name="fname"/>
<input type="submit" value="Click for page1.php!" onClick="submitFunction(1);return false;"/>
<input type="submit" value="Click for search_flights!" onClick="submitFunction(2);return false;"/>
</form>
</body>
</html>