首先打開Dreamweaver CS5,設計兩個表單,如:1.PHP,2PHP,最後把表單信息添加到數據庫。 javascript
新生錄入的表單頁面;新建了名爲1。PHP的表單,其中包括文本輸入框(學號、姓名、棟、房間、牀號、家庭地址、聯繫手機和家庭電話)、列表/菜單組件(年級、班級、專業、年、月、日)、單選按鈕(性別)以及兩個按鈕(一個是提交submit按鈕,另外一個是重置reset按鈕)。本頁還對學號與姓名兩個輸入框進行了javascript判斷,判斷二者不能爲空! php
2.php:獲取表單form1中的各個輸入項內容,並寫入數據表student中,若成功則返回「記錄已經成功添加,1秒後返回繼續錄入新生信息……」,不然提示「添加新數據出錯! css
打開Dreamweaver CS5;新建文件; html
輸入以下代碼,其中加粗的代碼爲JavaScript驗證語句與PHP語句: java
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> mysql
<html xmlns="http://www.w3.org/1999/xhtml"> sql
<head> 數據庫
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 服務器
<title>學生信息管理系統-新生錄入</title> 網絡
<style type="text/css"> /*網頁中提示信息的樣式設置*/
<!--
.STYLE1 {font-size: 36px}
-->
</style>
<link href="../mystyle.css" rel="stylesheet" type="text/css" />
<script language="javascript"> //對姓名與學號進行判斷
function valid_form(theForm)
{
if((theForm.stu_id.value=="")||(theForm.stu_name.value=""))
{
alert("出錯緣由:\n學生學號爲空!\n學生姓名爲空!");
theForm.stu_id.focus();
return false;
}
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="ch14-5-1.php" onsubmit="return valid_form(this)">
<p align="center" class="STYLE1">學生信息管理系統-新生錄入</p>
<table width="606" height="344" border="1" align="center" cellpadding="0" cellspacing="5">
<tr>
<td width="123" valign="top">
<p align="center">
<strong>輸入學號:</strong>
</p>
</td>
<td width="266" valign="top">
<label>
<input type="text" name="stu_id" id="stu_id" />
</label>
</td>
<td width="189" valign="top"><p>*不能爲空</p></td>
</tr>
<tr>
<td width="123" height="25" valign="top"><p align="center"><strong>輸入姓名:</strong></p></td>
<td width="266" valign="top">
<label>
<input type="text" name="name" id="name" />
</label>
</td>
<td width="189" valign="top">
<p>*不能爲空</p>
</td>
</tr>
<tr>
<td width="123" valign="top">
<p align="center"><strong>所在年級:</strong></p>
</td>
<td width="266" valign="top">
<label>
<select name="class" id="class">
<? for($i=2007; $i<=2012;$i++)
//循環顯示2007-2012間的數字作爲列表的選項值
{
echo "<option value=".$i."級>".$i."級</option>";
}
?>
</select>
</label>
</td>
<td width="189" valign="top"><p> </p></td>
</tr>
<tr>
<td valign="top">
<p align="center"><strong>所在班級:</strong></p>
</td>
<td valign="top">
<label>
<select name="classid" id="classid">
<? for($i=1; $i<=6;$i++)
//循環顯示1-6間的數字作爲列表的選項
{
echo "<option value=".$i."班>".$i."班</option>";
}
?>
</select>
</label>
</td>
<td valign="top"> </td>
</tr>
<tr>
<td width="123" valign="top">
<p align="center"><strong>選擇專業:</strong></p>
</td>
<td width="266" valign="top">
<label>
<select name="major" id="major">
<option value="計算機應用">計算機應用</option>
<option value="計算機軟件">計算機軟件</option>
<option value="計算機網絡">計算機網絡</option>
<option value="藝術設計">藝術設計</option>
<option value="電子商務">電子商務</option>
<option value="國際貿易">國際貿易</option>
<option value="市場營銷">市場營銷</option>
</select>
</label>
</td>
<td width="189" valign="top"><p> </p></td>
</tr>
<tr>
<td width="123" height="17" valign="top">
<p align="center"><strong>選擇宿舍:</strong></p>
</td>
<td width="266" valign="top">
<input name="building" type="text" id="building" size="4" /> 棟
<label>
<input name="room" type="text" id="room" size="4" />
</label> 房間
</td>
<td width="189" valign="top"><p> </p></td>
</tr>
<tr>
<td width="123" height="18" valign="top">
<p align="center"><strong>選擇牀號:</strong></p>
</td>
<td width="266" valign="top">
<label>
<input name="bed" type="text" id="bed" size="4" />
</label>
</td>
<td width="189" valign="top"><p> </p></td>
</tr>
<tr>
<td width="123" height="22" valign="top">
<p align="center"><strong>性 別:</strong></p>
</td>
<td width="266" valign="top">
<label>
<input name="sex" type="radio" id="sex" value="男" checked="checked" />
</label>
男
<label>
<input type="radio" name="sex" id="sex2" value="女" />
女</label>
</td>
<td width="189" valign="top"><p> </p></td>
</tr>
<tr>
<td width="123" height="21" valign="top">
<p align="center"><strong>出生日期</strong>:</p>
</td>
<td width="266" valign="top">
<label>
<select name="year" id="year">
<? for($i=1982; $i<=2008;$i++)
//循環顯示1982-2008間的數字作爲列表的選項
{
echo "<option value=".$i."年>".$i."</option>";
}
?>
</select>
年
<select name="month" id="month">
<? for($i=1; $i<=12;$i++)
//循環顯示1-12間的數字作爲列表的選項
{
echo "<option value=".$i."月>".$i."</option>";
}
?>
</select>
月
<select name="day" id="day">
<? for($i=1; $i<=31;$i++)
//循環顯示1-31間的數字作爲列表的選項
{
echo "<option value=".$i."日>".$i."</option>";
}
?>
</select>
日
</label></td>
<td width="189" valign="top"><p> </p></td>
</tr>
<tr>
<td width="123" valign="top">
<p align="center"><strong>家庭地址:</strong></p>
</td>
<td width="266" valign="top">
<label>
<input name="address" type="text" id="address" size="30" />
</label>
</td>
<td width="189" valign="top"><p> </p></td>
</tr>
<tr>
<td width="123" valign="top">
<p align="center"><strong>聯繫手機:</strong></p>
</td>
<td width="266" valign="top">
<label>
<input type="text" name="tel" id="tel" />
</label>
</td>
<td width="189" valign="top"><p>本身的手機號碼 </p></td>
</tr>
<tr>
<td width="123" valign="top">
<p align="center"><strong>家庭電話</strong>:</p></td>
<td width="266" valign="top">
<label>
<input type="text" name="tel2" id="tel2" />
</label>
</td>
<td width="189" valign="top"><p>家庭聯繫電話 </p></td>
</tr>
<tr>
<td valign="top"> </td>
<td align="left" valign="middle">
<input type="submit" name="button" id="button" value="錄入系統 " /> <input type="submit" name="button2" id="button2" value="從新錄入" />
</td>
<td valign="top"> </td>
</tr>
</table>
<p align="center">
<a href="ch14-5.php">查看學生信息</a>
</p>
</form>
</body>
</html>
第二步: 建立表單處理文件ch14-5-1.php
建立新文件,在Dreamweaver CS3編輯區,輸入以下代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>學生信息管理系統-新生錄入添加至數據庫</title>
</head>
<body>
<?
//獲取表單信息
$id=htmlspecialchars($_POST["stu_id"]);
$name=htmlspecialchars($_POST["name"]);
$class=htmlspecialchars($_POST["class"]);
$classid=htmlspecialchars($_POST["classid"]);
$major=htmlspecialchars($_POST["major"]);
$room=htmlspecialchars($_POST["building"])."棟".htmlspecialchars($_POST["room"])."房間";
$bedid=htmlspecialchars($_POST["bed"]);
$sex=htmlspecialchars($_POST["sex"]);
$birthday=trim(htmlspecialchars($_POST["year"]))."-".trim(htmlspecialchars($_POST["month"]))."-".trim(htmlspecialchars($_POST["day"]));
$address=htmlspecialchars($_POST["address"]);
$tel=htmlspecialchars($_POST["tel"]);
$tel2=htmlspecialchars($_POST["tel2"]);
//獲取爲空的數據時作相應的處理
if($room=="")
$room="未安排";
if($bedid=="")
$bedid="未安排";
if($address=="")
$address="暫缺";
if($tel=="")
$tel="暫缺";
if($tel2=="")
$tel2="暫缺";
//引用公用文件,鏈接服務器,選擇數據庫
require "config.inc.php";
//發送SQL請求,插入新數據
$sql="insert into $table_name(stu_id,name,classname,classid,major,room,bedid,sex,birthday,address,telephone,hometel) values('$id','$name','$class','$classid','$major','$room','$bedid','$sex','$birthday','$address','$tel','$tel2')";
//發送SQL請求
mysql_query($sql) or die("添加新數據時出錯".mysql_error());
//若成功執行插入語句,則執行下列操做:1秒後刷新跳轉
echo "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>";
echo "<meta http-equiv='refresh' content='1; url=ch14-5.php'>";
echo "</head>";
echo "<body>記錄已經成功添加,1秒後返回繼續錄入新生信息……</body>";
echo "</html>";
?>
</body>
</html>
第三步:保存文件並調試運行
點擊「文件」-「保存」,將兩個文件分別保存爲11.PHP,12.PHP。
<form id="form1" name="form1" method="post" action="ch14-5-1.php" onsubmit="return valid_form(this)">
打開11.PHP,按f12對網頁進行調試。
如圖;
首先來測試其中的JavaScript是否正確,在不輸入任何數據的狀況下,點擊「錄入系統」按鈕,會彈出如圖的警告框。
點擊「錄入系統」按鈕,若添加數據到數據庫中成功,則顯示如圖14-22所示的信息,不然顯示「添加新數據時出錯」以及錯誤信息:
點擊下方的「查看學生信息」,咱們能夠進入11.PHP,會看到在數據表student的最後添加了一條新記錄,圖: