php与html之间变量使用问题

时间:2022-10-28 19:16:07
我原本想做个留言板,然后留言板中实现检测敏感词的功能,如果发现含有敏感词,那么会提示,并停留在原来的页面,保持原来的内容,以及重置后,保留姓名和留言标题。
下面是重置后的效果,并没有保留原来的姓名和标题啊。
php与html之间变量使用问题
一下是实现的代码:
html代码:
<?php
require('test2.php');
?>
<!doctype html>
<html>
<head>
<meta charset="utf8">
<title>留言板</title>
</head>
<style type="text/css">
#div1{margin-left:100px;margin-right:100px;margin-top:50px;width:800px 80%;height:500px;background:#dddddd;border:1px solid}
#div2{margin-left:40px;}
#div3{width:200px 100%;height:25px;}
#div4{width:300px;height:25px;}
#div5{width:650px;height:200px;}
#div6{margin-left:200px;height:25px;width:80px;}
#div7{margin-left:50px;height:25px;width:80px;}
</style>
<body>
<div id="div1">
<h1 align="center">我要留言</h1>
<hr align="center" width="90%">
<form id="div2" action="test2.php" method="post">
姓&nbsp;名:<input id="div3" type="text" name="name" value="<?php echo $new_name; ?>">&nbsp;*
<br/><br/>
标&nbsp;题: <input id="div4" type="text" name="title" value='<?php echo $new_title;?>'>&nbsp;*
<br/><br/>
内&nbsp;容: <textarea id="div5" type="text" name="content" value='<?php echo $new_content;?>'></textarea>&nbsp;*
<br/><br/>
<input id="div6" type="submit" name="submit" value="提交留言">
<input id="div7" type="submit" name="reset" value="重置留言">
</form>
</div>
</body>
</html>
以下是php代码:
<meta charset="utf8">
<?php
if(isset($_POST['submit'])){
$name=$_POST['name'];
$title=$_POST['title'];
$content=$_POST['content'];
$result=preg_match("/敏感词/",$content);
$new_name=$new_title=$new_content="";
if($result){
echo "<script type='text/javascript'>alert('留言内容中含有敏感词');</script>";
$new_name=$name; $new_title=$title; $new_content=$content;
}else{
echo <<<STR
<script type="text/javascript">
alert("提交留言成功");
</script>
STR;
    $fp=fopen("C:\Users\Administrator\Desktop\learning\php\content.txt",'a');
$outputstring=$name."\t".$title."\t".$content."\n";
fwrite($fp,$outputstring,strlen($outputstring));
fclose($fp);
}
}
if(isset($_POST['reset'])){
$name=$_POST['name'];
$title=$_POST['title'];
$new_name=$name;
$new_title=$title;
$new_content="";
}
echo<<<STR
<script type="text/javascript">
window.location.href="test2.html";
</script>
STR;
?>
求各位大神帮忙解决下问题啊!!

4 个解决方案

#1


你这个问题最好的方式是用ajax的方式,表单提交前验证,button的type=button而不是submit。具体例子你需要我在给你写,建议个人解决。学到的东西会更多,大的方向就是用ajax去做

#2


服务器配置的问题,并没有解析里面的php代码而是直接输出了,你的文件是不是html结尾?
换成php结尾,访问试试呢?

#3


引用 2 楼 hellodifa 的回复:
服务器配置的问题,并没有解析里面的php代码而是直接输出了,你的文件是不是html结尾?
换成php结尾,访问试试呢?


应该不是服务器问题,因为我在另一个页面测试,都可以输出php的echo,但就是在html中就不行了,不明白这是为啥。如何换成php结尾啊,还是个菜鸟,还望指教。

#4


你这是点重置之后会出现这个问题,还是一进页面就出现这个问题?还有你的跳转有点乱,既然你已经在test2.html中require包含了test2.php了,form表单的action就不要再跳到test2.php了,就直接跳test.html吧,再把test2.php里面用js写的跳到test2.html也注释掉吧,你包含了test2.php那其实就已经在同一个页面了。

#1


你这个问题最好的方式是用ajax的方式,表单提交前验证,button的type=button而不是submit。具体例子你需要我在给你写,建议个人解决。学到的东西会更多,大的方向就是用ajax去做

#2


服务器配置的问题,并没有解析里面的php代码而是直接输出了,你的文件是不是html结尾?
换成php结尾,访问试试呢?

#3


引用 2 楼 hellodifa 的回复:
服务器配置的问题,并没有解析里面的php代码而是直接输出了,你的文件是不是html结尾?
换成php结尾,访问试试呢?


应该不是服务器问题,因为我在另一个页面测试,都可以输出php的echo,但就是在html中就不行了,不明白这是为啥。如何换成php结尾啊,还是个菜鸟,还望指教。

#4


你这是点重置之后会出现这个问题,还是一进页面就出现这个问题?还有你的跳转有点乱,既然你已经在test2.html中require包含了test2.php了,form表单的action就不要再跳到test2.php了,就直接跳test.html吧,再把test2.php里面用js写的跳到test2.html也注释掉吧,你包含了test2.php那其实就已经在同一个页面了。