对比一下html和php文件分开和自处理表单的一些简单区别
分开的html和php代码
<html>
<head>
<title>first html form</title>
</head>
<body bgcolor=yellow>
<form action="/biaodan/test.php" method="GET"> // 1
<p/><b>Type your name here:</b>
<input type="text" name="namestring" size=50>
<p/><b>Talk about yourself here:</b><br />
<textarea name="comments" rows=5 cols=50>i was born...</textarea>
<p/><b> Choose your food:</b>
<input type="radio" name="choice" value="burger"/>Hamburger
<input type="radio" name="choice" value="fish"/>Fish
<input type="radio" name="choice" value="steak"/>Steak
<input type="radio" name="choice" value="yogurt"/>Yogurt
<p/><b>Choose a work place:</b><br />
<input type="checkbox" name="place1" value="LA" />Los Angeles
<br/>
<input type="checkbox" name="place2" value="SJ" />San Jose
<br />
<input type="checkbox" name="place3" value="SF" Checked>San Francisco
<p/><b>Choose a vacation spot:</b>
<select name="location">
<option selected value="hawaii"/>Hawaii
<option value="bali"/>Bali
<Option value="maine"/>Maine
<option value="paris"/>Paris
</select>
<p/><input type="submit" value="submit">
<input type="reset" value="clear">
</form>
</body>
</html>
<?php
echo "Your name", stripslashes($_GET['namestring']) , "<br/>";
echo $_GET['comments'], "<br/>";
echo "choosed food:" ,$_GET['choice'],"<br/>";
echo "choosed work place:" ,$_GET['place1'],"<br/>";
echo "choosed vacation spot:" ,$_GET['location'],"<br/>";
?>
<?php
foreach($_GET as $key => $value)
{
echo "<b>$key</b><br/>";
}
echo stripslashes("\'adada");
?>
自处理表单
<?php
if(isset($_POST['submit']))
{
print "Your name is <b>$_POST[your_name]</b><br />";
print "Your phone is <b>$_POST[your_phone]</b><br />";
}
else
{ ?>
<html>
<head><title>HTML Form</title></head>
<body bgcolor="lightblue"><font size="+1">
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
<p />
Please enter your name: <br />
<input type="text" size=50 name="your_name">
Please enter your phone: <br />
<input type="text" size=50 name="your_phone">
<input type="submit" name="submit" value="Send Now">
<input type=reset value="Clear">
</form>
</body>
</html>
<?php } ?>
显然自处理表单的action是自己这个文件,在php中可以通过$_SERVER['PHP_SELF']来获取,而表单和php分开的话,action这里你需要输入文件的路径;还有区别就是自处理表单的文件开头需要通过isset()来判断。
|