PHP与Web页面交互
PHP中提供了两种与Web页面交互的方法,一种是通过Web表单提交数据,另一种是通过URL参数传递。
表单提交用户名字和密码:
<form name "form1" method="post" action="">
UserName:<input type="text" name="user" size="20"></td>
PassWord:<input name="pwd" type="password" id="pwd" size="20"></td>
<input name="submit" type="submit" id="submit" value="login">&nbps;</td>
</tr>
</form>
<?php
if($_POST["submit"] == "login"){
echo "User:".$_POST['user']." Password:".$_POST['pwd'];
}
?>
单选按钮选择性别
<form action="" method="post" name="form1">
sex:<input name="sex" type="radio" value="1" checked>Man<input name="sex" type ="radio" value="0">Woman
<input type="submit" name="Submit" value="submit">
</form>
<?php
echo "esx:".$_POST["sex"];
?>
复选框选择爱好
<form name="form1" method="post" action="">
<table width="445" cellpadding="0" cellspacing="0">
<tr>
<td width="443" height="41" align="center" valign="top">ni xi huan de tu shu lei xing:
<input type="checkbox" name="mrbool[]" value="rumen"> rumen
<input type="checkbox" name="mrbool[]" value="anli"> anli
<input type="checkbox" name="mrbool[]" value="jiangjie"> jiangjie
<input type="checkbox" name="mrbool[]" value="shili"> shili
<input type="submit" name="submit" value="submit">
</td>
</tr>
</table>
</form>
<?php
if($_POST["mrbool"] != null){
echo "ni xuan ze de jie guo shi:";
for($i = 0 ;$i < count($_POST["mrbool"]) ;$i ++) {
echo $_POST["mrbool"][$i] . " ";
}
}
?>
下拉列表选择
<form name="form1" method="post" action="">
<table width="280" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="80" height="20" align="center"> <span class="stype2">yijianzhuti:</span> </td>
<td width="194">
<select name="select" size="1">
<option value="gongsifazhan" selected>gongsifazhan</option>
<option value="guanlizhidu">guanlizhidu</option>
<option value="houqinfuwu">houqinfuwu</option>
</select>
<input type="submit" name="submit" value="submit">
</td>
</tr>
</table>
</form>
<?php
if($_POST["select"] != ""){
echo "ninxuanzedeyijianzhutiwei:<br>";
echo $_POST["select"]."
";
}
?>
菜单栏选择(可多选)
<form name="form1" method="post" action="">
<table width="300" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="30" align="center" valign="middle">qing xuan ze xi huan de PHP lei tu shu</td>
</tr>
<tr>
<td align="center" valign="middle"><select name="select[]" size="5" multiple>
<option value="11111">11111</option>
<option value="22222">22222</option>
<option value="33333">33333</option>
<option value="44444">44444</option>
<option value="55555">55555</option>
</select></td>
</tr>
<tr>
<td height="30" align="center" valign="middle">
<input type="submit" name="Submit" value="submit">
</td>
</tr>
</table>
</form>
<?php
if($_POST["Submit"] == "submit"){
echo "ninxuanzedeyijianzhutiwei:<br>";
for($i = 0;$i < count($_POST["select"]) ;$i ++){
echo $_POST["select"][$i]."
";
}
}
?>
获取文件域的值
<!-accept可以指定文件类型-!>
<!-本例子并没有真是上传文件,所以没有写元素enctype="multipart/form-data"-!>
<form name="f" method="post" action="">
<input type="file" name="file" size="15">
<input type="submit" name="upload" value="up">
</form>
<?php
echo $_POST["file"];
?>
URL参数编码/解码
<a href="demo.php?id=<?php echo urlencode("aaa a编码")?>">PHPLINK</a>
//http://localhost:63342/untitled/demo.php?id=aaa+a%E7%BC%96%E7%A0%81
<?php
header("Content-Type: text/html;charset=utf-8");
//echo urlencode("aaaa你好");
echo urldecode(urlencode("aaaa你好"));
?>
最后无聊,写了一个评论的东西。简单的模拟而已,用文件存的。并且没有各种ip次数什么的限制。就是为了练习下相关表单提交。
Index.php是主界面
WriteATalk.php是提交处理界面,直接就是表单提交到这里,存下文件而已。
Index.php
<form name "form1" method="post" action="WriteATalk.php">
Say Something:<br>
<textarea name="tktk" id="tktk" rows="10" cols="40" ></textarea>
<input name="submit" type="submit" id="submit" value="Submit">
</form>
<?php
header("Content-Type: text/html;charset=utf-8");
function ShowTalk()
{
$fp = fopen("talk.dat", "a+");
if ($fp) {
while (!feof($fp)) {
echo fgets($fp)."<br>";
}
fclose($fp);
}
return ;
}
ShowTalk();
?>
WriteATalk.php
<?php
header("Content-Type: text/html;charset=utf-8");
function AddOneTalk($str_message){
$fp = fopen("talk.dat", "a+");
if(!($fp)){
return 0;
}
$flag=fwrite($fp,$str_message."
");
fclose($fp);
return $flag;
}
$str_message = date("Y/m/d h:i:s a")." ( ".$_SERVER["REMOTE_ADDR"]." )"."<br>";
$str_message =$str_message." ".$_POST["tktk"]."<br>";
AddOneTalk($str_message);
echo"以下内容已经成功提交:<br>".$str_message."<br>";
echo "<a href='index.php'>返回上一页面</a>";
?>