2014-02-19 46 views
-3

我想写一个简单的PHP窗体,但出来是不同于我想要它是。有没有人看到我的错误是多谢,并感谢。 作业: 编写一个PHP脚本,用于检查从表单发送的消息,然后打印发件人和消息的名称。如果发件人姓名或消息为空,请打印“您没有提供所有必需的信息!”删除用户名或消息之前或之后的任何空格。也删除任何HTML标签,以确保用户不能改变留言簿。所使用的形式如下:简单的PHP窗体1

<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. 

回答

0

您还没有命名输入“提交”,唯一一家拥有提交,这是你在找什么。尝试在if语句,而不是使用isset($_GET['name']),因为这是你实际上使用的是什么:

<?php 
if(isset($_GET['name'])) 
{ 
    echo $_GET['name'].": Hello!"; 
} 
else 
{ 
    echo "You didn’t give all required information!"; 
} 
?> 

你应该知道,虽然像这样的直接输出用户输入的数据是非常不安全的,创造一个非常大的XSS漏洞。

+0

嗨,感谢您的帮助,当我尝试您的代码输出是不同的我们的程序不能正常运行 您的程序生成以下输出: John:您好! \t \t 应生成以下输出: 您没有提供所有必需的信息! 白色区域表示程序输出正确。 表示回车 在输出的比较中,程序的输出必须与示例输出完全相同。 – user3314727

+0

您有原始问题未指定的附加要求。这听起来像你的要求是在显示消息之前检查_both_名称_and_消息是否已设置。尝试改变这行以说'if(isset($ _ GET ['name'])&& isset($ _ GET ['message'])){'。 – Adam

0
if(isset($_GET['submit'])) 

应该

if(isset($_GET['name'])) 
0

你的PHP看起来应该像下面这样:

<?php 
$name = $_GET['name']; 
if($name != ''){ 
echo "$name: Hello!"; 
} 
else 
{ 
echo "You didn’t give all required information!"; 
} 
?> 

注:我没有测试过这一点。

+0

应该指出,这将不兼容[PHP的严格水平](http://lampgurus.com/site/b/2012/02/18/strict-mode-in-php.html)兼容 - 如果页面加载时没有'name'发送GET参数会触发警告。为'$ _GET ['name']'添加'isset'检查可以防止这种情况发生。 – Adam

0

在你的声明:

如果发件人名称或邮件是空白,打印“你没有给所有 需要的信息!”之前或用户名 或邮件后删除任何空格。也删除任何HTML标签,以确保用户不能更改留言簿。

,你应该有这样的事情里面你的if语句:

if(isset($_GET['name'])){ 

    $clean_name = trim(strip_tags($_GET['name'])); 

    echo $clean_name; 

} 

装饰()删除多余的空格前后串的后用strip_tags()删除HTML标签

0
<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!"; 
    } 
} 
?>