php表單採java驗證多選資料狀況

新年快樂~~
最近練習的是製作一份電腦申請表單,必填欄位的部分是用JAVA來進行欄位空值驗證,
但是當PHP表單欄位改為多選時,驗證就出現異常,且表單也無法送出,不知問題出在哪裡,還請大家協助指點一下迷津,謝謝!
<!DOCTYPE HTML> 
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>變更申請</title>
<head>
<script type="text/javascript" language="JavaScript">
function check2()
{	if(ca2.empnb.value == "") 
    {
      alert("未選擇申請人");
      return false;
    }
    x ="" ;  y = 0
    a ="" ;  b = 0
	for ( i = 0;  i <= (document.ca2.name.length-1);  i++ )
		{  if(document.ca2.name[i].selected)
			{	x += (document.ca2.name[i].value + " , " )
				y += 1	}
		}
	if (y==0)   {	 
	             alert("請選擇使用者姓名 ! " );
				 return false;
      		    }
	for ( j = 0;  j <= (document.ca2.cp.length-1);  j++ )
		{  if(document.ca2.cp[j].selected)
			{	a += (document.ca2.cp[j].value + " , " )
				b += 1	}
		}
	if (b==0)   {	 
	             alert("請選擇電腦主機編號 ! " );
				 return false;
				}
	else	{  return false; }
}
</script>
</head>
<body>
<body bgcolor="#FFFFCC" text="black" link="blue" vlink="purple" alink="red" leftmargin="90" marginwidth="10" topmargin="10" marginheight="10">
<form action="ca2.php" method="post" name="ca2">
    <p><font size="5" color="blue"><i><b><u>變更申請</u> &nbsp;&nbsp;</b></i></font><font size="2" color="red">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(紅色 </font><font size="2" face="新細明體" color="red">※ 標示為必填或必選項目</font><font size="2" color="red">) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font face="新細明體" color="red">&nbsp;&nbsp; </font><font face="新細明體"
color="black">申請日期:</font>
    <script language="javascript">
 var Today=new Date();
 document.write(Today.getFullYear()+ " 年 " + (Today.getMonth()+1) + " 月 " + Today.getDate() + " 日");   
    </script>
<font face="新細明體" color="red"><span style="line-height:100%;"> </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></p>
    <p><font face="新細明體" color="red"><br>
</font><font face="新細明體" color="black">申請人:</font><select name="empnb" size="1" style="font-family:新細明體; color:blue; background-color:rgb(255,255,204);">
        <option selected></option>
		<option value="周捷倫">周捷倫</option>
        <option value="蔡依林">蔡依林</option>
        <option value="小頻果">小頻果</option>
        <option value="大巨蛋">大巨蛋</option>
        <option value="奧斯卡">奧斯卡</option>
</select><font face="新細明體" color="black"> </font><font face="新細明體" color="red">※</font><font face="新細明體" color="red"><br>
<br>
<br>
</font><font face="新細明體" color="black">使用者姓名:</font><select name="name[]" size="6" multiple style="font-family:新細明體; color:blue; background-color:rgb(255,255,204);">
		<option value="周捷倫">周捷倫</option>
        <option value="蔡依林">蔡依林</option>
        <option value="小頻果">小頻果</option>
        <option value="大巨蛋">大巨蛋</option>
        <option value="奧斯卡">奧斯卡</option>
		<option value="金馬獎">金馬獎</option>
</select><font face="新細明體" color="black"> &nbsp;</font><font face="新細明體" color="red">※</font><font size="2" face="新細明體" color="black"><span style="line-height:100%;">(按下Ctrl鍵+滑鼠左鍵可選擇多人) </span></font><font face="新細明體" color="black">&nbsp;</font><font face="新細明體" color="red">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font
face="新細明體" color="black"> 電腦主機編號:</font><select name="cp[]" size="6" multiple style="font-family:新細明體; color:blue; background-color:rgb(255,255,204);">
		<option value="001">001</option>
        <option value="002">002</option>
        <option value="003">003</option>
        <option value="004">004</option>
        <option value="005">005</option>
        <option value="006">006</option>
</select><font face="新細明體" color="black"> &nbsp;</font><font face="新細明體" color="red">※</font><font size="2" face="新細明體" color="black"><span style="line-height:100%;">(按下Ctrl鍵+滑鼠左鍵可選取多台工作站)</span></font><font size="2" face="新細明體" color="red"><span style="line-height:100%;"><br>
<br>
<font face="新細明體">● 軟體安裝:</font><select name="soft[]" size="6" multiple style="font-family:新細明體; color:blue; background-color:rgb(255,255,204);">
        <option value="ZWCAD">ZWCAD</option>
        <option value="FAX">FAX</option>
        <option value="DESK">DESK</option>
        <option value="OFFICE 2003">OFFICE 2003</option>
	<option value="OFFICE 2010">OFFICE 2010</option>
        <option value="OFFICE 2013">OFFICE 2013</option>
</select><font size="2" face="新細明體" color="red"><span style="line-height:100%;">(按下Ctrl鍵+滑鼠左鍵可選取多套軟體) </span></font><font face="新細明體" color="red">&nbsp;</font><br>
<font face="新細明體"><br>
● 其他需求:<input type="text" name="other" maxlength="90" size="90" style="font-family:新細明體; color:blue; background-color:rgb(255,255,204);"></font></p>
    <p>&nbsp;</p>	    
</form>
<input type="button" name="forwork" value="送出申請" onClick="check2()" />
</body>
</html>

評論

  • 1. 你想說的應該是 javascript ,跟 java 是兩種截然不同的東西
    2. 下拉單選取值應該是從 ca2.empnb.options[ca2.empnb.selectedIndex] 去取得選到的項目
    3. 多選則是以 options 跑迴圈去檢查 selected ,你的程式沒有定義 id 也少了一層
    4. 可以考慮用 jquery 之類的架構去處理,當然更潮一點就玩一下 angularjs or react
Sign In or Register to comment.