简单php表单1
我试图编写一个简单的php表单,但是输出与我想要的不同。有人知道我的错误是什么吗?谢谢并感谢。 作业: 编写一个PHP脚本,检查表单发送的消息,然后打印发送者的姓名和消息。如果发件人姓名或邮件为空,请打印“您没有提供所有必需的信息!”删除用户名或消息前后的所有空格。同时删除所有HTML标记,以确保用户无法更改留言簿。使用的表单如下所示:简单php表单1,php,Php,我试图编写一个简单的php表单,但是输出与我想要的不同。有人知道我的错误是什么吗?谢谢并感谢。 作业: 编写一个PHP脚本,检查表单发送的消息,然后打印发送者的姓名和消息。如果发件人姓名或邮件为空,请打印“您没有提供所有必需的信息!”删除用户名或消息前后的所有空格。同时删除所有HTML标记,以确保用户无法更改留言簿。使用的表单如下所示: <form action="guestbook.php" method="get"> Sender: <input TYPE="t
<form action="guestbook.php" method="get">
Sender: <input TYPE="text" name="name"><br>
Message: <input type="text" name="message"><br>
<input type="submit" value="Send">
</form>
Example output
John: Hello!
**my script:**
<?php
$name = $_GET["name"];
if(isset($_GET['submit'])) {
echo "$name: Hello!";
} else {
echo "You didn’t give all required information!";
}
?>
YOUR PROGRAM DOES NOT OPERATE CORRECTLY
Your program generated the following output:
You didn’t give all required information!
The following output should have been generated:
John: Hello!
The white area indicates correct output from your program.
represents a carriage return
In the comparison of outputs, the output of your program must be exactly the same as the example output.
发件人:
信息:
示例输出
约翰:你好!
**我的剧本:**
您的程序运行不正常
您的程序生成了以下输出:
你没有提供所有必要的信息!
应已生成以下输出:
约翰:你好!
白色区域表示程序的正确输出。
表示回车
在比较输出时,程序的输出必须与示例输出完全相同。
您没有名为“提交”的输入,只有一个带有提交类型的输入,这正是您要查找的内容。请尝试在if语句中使用isset($\u GET['name'])
,因为您实际使用的是:
<?php
if(isset($_GET['name']))
{
echo $_GET['name'].": Hello!";
}
else
{
echo "You didn’t give all required information!";
}
?>
您应该知道,像这样直接输出用户输入的数据是非常不安全的,这会造成很大的漏洞
if(isset($_GET['submit']))
应该是
if(isset($_GET['name']))
您的PHP应该如下所示:
<?php
$name = $_GET['name'];
if($name != ''){
echo "$name: Hello!";
}
else
{
echo "You didn’t give all required information!";
}
?>
注意:我没有对此进行测试。在您的声明中:
如果发件人姓名或邮件为空,请打印“您没有提供所有信息”
所需信息!”删除用户名之前或之后的所有空格
或消息。同时删除所有HTML标记,以确保用户无法
更改留言簿
您应该在if语句中包含以下内容:
if( isset($_GET['name']) ){
$clean_name = trim(strip_tags($_GET['name']));
echo $clean_name;
}
trim()删除字符串前后的额外空格,并删除标签()删除html标签
<form action="guestbook.php" method="get">
Sender: <input type="text" name="name"><br>
Message: <input type="text" name="message"><br>
<input type="submit" value="Send" name="send">
</form>
<?php
if(isset($_GET['send'])) {
$name = trim(strip_tags($_GET['name']));
if($name != ''){
echo "$name: Hello!";
} else {
echo "You didn’t give all required information!";
}
}
?>
发件人:
信息:
应注意,这与compatible不兼容–如果加载页面时未发送名称
GET参数,则会触发警告。添加一个isset
检查$\u GET['name']
将防止这种情况。您好,谢谢您的帮助,当我尝试您的代码时,输出不同,我们的程序运行不正常您的程序生成了以下输出:John:您好!应该生成以下输出:您没有提供所有必需的信息!白色区域表示程序的正确输出。表示回车。在比较输出时,程序的输出必须与示例输出完全相同。您有原始问题未指定的其他要求。听起来您的要求是在显示消息之前检查名称和消息是否已设置。尝试将行更改为if(isset($\u GET['name'])和&isset($\u GET['message']){
。