![]() ![]() ![]() ![]() |
|||||
|
|||||
樓主 小佑 ![]()
![]() |
程式主要html和處理表單的程式PHP 如下: program.html [code] <!doctype html> <html> <head> <meta charset="utf-8"> <script type="text/javascript" src="utility.js"></script> <script type="text/javascript"> var XHR = null; var name; var sex ; var school ; var interest ; var thought ; var vars; function startRequest() { XHR = createXMLHttpRequest(); url="reply.php"; name = document.getElementById("UserName").value; sex = document.getElementById("UserSex").value; school = document.getElementById("UserSchool").value; interest = document.getElementById("UserInterest").value; thought = document.getElementById("UserThought").value; vars= "UserName="+name+"&UserSex="+sex+"&UserSchool="+school+"&UserInterest[]="+interest+"&UserThought="+thought; XHR.open("post",url, true); XHR.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); XHR.onreadystatechange = handleStateChange; XHR.send(vars); } function handleStateChange() { if (XHR.readyState == 4) { if (XHR.status == 200) document.getElementById("span1").innerHTML = XHR.responseText; else window.alert("檔案開啟錯誤!"); } } function clearn(){ document.getElementById("span1").innerHTML = ""; } </script> </head> <body> <form method="post" action="reply.php"> <fieldset> <legend>個人資訊</legend> 姓名: <input type="text" name="UserName" id="UserName" size="20"><br> 性別: <input type="radio" name="UserSex" id="UserSex" value="Male">男 <input type="radio" name="UserSex" id="UserSex" value="Female">女<br> 最高學歷: <select name="UserSchool" id="UserSchool" size="4"> <option value="高中以下">高中職或以下 <option value="大專院校">大專院校 <option value="碩士">碩士 <option value="博士">博士 <option value="其它">其它 </select><br> </fieldset> <p></p> <fieldset> <legend>其它資訊</legend> 您喜歡下列哪些活動(可複選)? <input type="checkbox" name="UserInterest[]" id="UserInterest" value="閱讀">閱讀 <input type="checkbox" name="UserInterest[]" id="UserInterest" value="運動">運動 <input type="checkbox" name="UserInterest[]" id="UserInterest" value="看電影">看電影 <input type="checkbox" name="UserInterest[]" id="UserInterest" value="旅行">旅行<br> 您對於近年興起的國片風潮有何看法?<br> <textarea name="UserThought" id="UserThought" cols="45" rows="4"></textarea><br> </fieldset> <p><input type="button" value="傳送資料" onclick="startRequest()"> <input type="reset" value="清除資料" onclick="clearn()"></p> </form> <span id="span1"> </body> </html> [/code] reply.php [code] <?php $Name = $_POST["UserName"]; $Sex=$_POST["UserSex"]; switch($_POST["UserSex"]) { case "Male": $Sex = "男"; break; case "Female": $Sex = "女"; } $School = $_POST["UserSchool"]; $Thought = $_POST["UserThought"]; $sport = $_POST ["UserInterest"]; $myallsport = implode ($sport); echo '<p><b><i>'.$Name.'</b></i>,您好!謝謝您填寫個人資料表,您輸入的資料如下:</p>'; echo '<p>性別:'.$Sex.'</p>'; echo '<p>最高學歷:'.$School.'</p>'; echo '<p>您喜歡的活動:'.$myallsport.'</p>'; echo '<p>您對於國片風潮的看法:'.$Thought ; [/code] utility.js function createXMLHttpRequest() { try //其它非IE的瀏覽器 { var XHR = new XMLHttpRequest(); } catch(e1) { try //IE6+瀏覽器 { var XHR = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e2) { try //IE5瀏覽器 { var XHR = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e3) //不支援Ajax { XHR = false; } } } return XHR; } |
別忘捐VP感謝幫助你的人 | 新手會員瞧一瞧 |
1樓 |
checkbox跟radio的值讀取方式,並不是用value來取值的。
你可以google一下取值的方式。 如果肯學的話。學一下用jquery處理。會比較方便。 而且jquery有一個插件from。 可以幫你讓表單的發送是用ajax的模式處理。 此值的發送會幫你自動取得並調整。
本篇文章回覆於2017-10-27 17:27
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔-- |
回覆 |
如要回應,請先登入. |