您的位置:首页 > 编程语言 > PHP开发

<php+mysql>PHP基础,使用PHP访问表单数据

2015-12-24 09:46 771 查看
(《Head First PHP & MySQL》学习记录)



HTML5:<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>report</title>
<link rel="stylesheet" href="report.css" />
</head>
<h1>Aliens Abducted Me - Report an Abduction</h1>
<p>Share your story of alien abduction:</p>
<form method="post" action="report.php">
<label for="firstname">First Name: </label>
<input type="text" id="firstname" name="firstname" /><br />
<label for="lastname">Last Name: </label>
<input type="text" id="lastname" name="lastname" /><br />
<label for="email">Email: </label>
<input type="text" id="email" name="email" /><br />
<label for="when">When did it happened: </label>
<input type="text" id="when" name="when" /><br />
<label for="timelast">How long were you gone:</label>
<input type="text" id="timelast" name="timelast" /><br />
<label for="fang">Have you seen my dog Fang?</label>
Yes<input type="radio" id="fang" name="fang" value="yes" />
No<input type="radio" id="fang" name="fang" value="no" /><br />
<label for="anythingelse">Anything else to add?</label>
<input type="text" id="anythingelse" name="anythingelse" /><br />
<input type="submit" name="submit" value="Report Abduction" />
</form>
<body>
</body>
</html>
PHP:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>report</title>
</head>

<body>
<h2>Aliens Abducted Me - Report an Abduction</h2>
<?php
$name=$_POST['firstname'].' '.$_POST['lastname'];
$when=$_POST['when'];
$timelast=$_POST['timelast'];
$fang=$_POST['fang'];
$anythingelse=$_POST['anythingelse'];
$email=$_POST['email'];

$to='changingvan@163.com';
$subject='Aliens abducted me';
$msg="$name was abducted $when and was gone for $timelast.\n".
"Fang spotted: $fang.\n".
"Other info: $anythingelse.\n";
mail($to,$subject,$msg,'Form'.$email);

echo 'Thanks for submitting the form. <br/>';
echo 'You were abducted '.$when;
echo 'and gone for '.$timelast.'<br/>';
echo 'You saw Fang? '.$fang.'<br/>';
echo 'Other information: '.$anythingelse.'<br/>';
echo 'Your email address is '.$email;
?>
</body>
</html>
代码下载:http://download.csdn.net/detail/qq_17615475/9374869
php是工作于服务器端的,浏览器无法处理,所以要正常运行这个代码需要在计算机上搭建服务器,具体操作见:

<php+mysql>Mac配置APACHE+PHP+MYSQL+PHPMYADMIN http://blog.csdn.net/qq_17615475/article/details/50384817

然后将下载的代码放进上文中配置的用户级目录,在浏览器中输入localhost/文件夹/report.html地址来运行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: