经过了一周的工作,老师同学们可以在学海灯塔上给我们留言了。欢迎光临我们的学海灯塔。
下面介绍一下这个功能是怎么实现的。
contact.php
<form action="contact_handle.php" method="post" class="contact_form">
<h2>联系我们</h2>
<div class="col-md-6 grid_6">
<input name="name" type="text" class="text" id="name" placeholder="name" οnfοcus="this.value = '';" οnblur="if (this.value == '') {this.value = '姓名(选填)';}" value="姓名(选填)">
<input name="email" type="text" class="text" id="email" placeholder="email" οnfοcus="this.value = '';" οnblur="if (this.value == '') {this.value = '邮箱(选填)';}" value="邮箱(选填)">
<input name="tel" type="text" class="text" id="tel" placeholder="phone" οnfοcus="this.value = '';" οnblur="if (this.value == '') {this.value = '电话(选填)';}" value="电话(选填)">
</div>
<div class="col-md-6 grid_6">
<textarea name="advice" id="advice" placeholder="Your Advice" οnfοcus="this.value = '';" οnblur="if (this.value == '') {this.value = '您的建议';}" value="Your Advice">您的建议</textarea>
</div>
<div class="clearfix"> </div>
<input type="submit" name="submit" value="发送" />
</form>
首先在contact.php这个页面里面有一个表单,用户把信息和建议填写好之后可以点击提交。效果如下。
接下来转到contact_handle.php进行处理。
contact_handle.php
<?php
$name=$_POST['name'];
$email=$_POST['email'];
$tel=$_POST['tel'];
$advice=$_POST['advice'];
$myfile = fopen("advice.txt", "a") or die("Unable to open file!");
fwrite($myfile, "name:".$name."\r\n");
fwrite($myfile, "email:".$email."\r\n");
fwrite($myfile,"tel:".$tel."\r\n");
fwrite($myfile,"advice:".$advice."\r\n");
fwrite($myfile,"\r\n");
fclose($myfile);
echo "<script>alert('感谢您的建议!');history.go(-1)</script>";
?>
通过这一个语句$myfile = fopen("advice.txt", "a")把文件打开,格式为在文件末尾写文件。fwrite($myfile, "name:".$name."\r\n")用来把内容写入文件。最后只要把文件关上就可以了。我们可以在后台看到老师同学们留下的宝贵的建议和意见。
到现在,我们的学海灯塔建设越来越完整了,欢迎大家访问我们的学海灯塔。