PHP脚本未在表单标记内运行

时间:2014-03-18 09:17:05

标签: php html

我最近安装了Apache 2.4.7服务器,以及PHP 5.5.10。我刚刚开始学习PHP。我目前正在研究w3school的PHP表单处理教程,但我无法得到一个特定的例子。这里有人看到错误吗?

<!DOCTYPE html>
<html>
<head>
    <title>PHP Test</title>
</head>
<body>
<?php
    // define variables and set to empty values
    $name = $email = $gender = $comment = $website = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
        $name = test_input($_POST["name"]);
        $email = test_input($_POST["email"]);
        $website = test_input($_POST["website"]);
        $comment = test_input($_POST["comment"]);
        $gender = test_input($_POST["gender"]);
    }

    function test_input($data)
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
?>
    <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">

        Name: <input type="text" name="name"><br>
        E-mail: <input type="text" name="email"><br>
        Website: <input type="text" name="website"><br>
        Comment: <textarea name="comment" rows="5" cols="40"></textarea><br>

        Gender:
        <input type="radio" name="gender" value="female">Female
        <input type="radio" name="gender" value="male">Male<br>
        <input type="submit">
    </form>
</body>
</html>

当我访问此网站时,我会在输入字段名称之前获得引号和大于号:“&gt;名称。当我提交表单时,URL显示为

http://127.0.0.1/%3C?php%20echo%20$_SERVER[

我无法输入网址的确切内容,因为这个网站不会让我,但%3C实际上显示为小于标志。 %20显示为空格。所以,问题是action标签内的php脚本没有运行。然后使用php脚本而不是当前页面的位置填充操作变量。为什么PHP脚本没有在我的表单标签中运行?

解决方案: 谢谢大家的帮助。 Abhik Chakraborty你的评论让我找到了解决方案。不,我没有用.php扩展名保存我的文件。我将扩展名更改为.php,它运行得很好。我会发布这个作为解决方案,但我必须等待八个小时,因为我没有足够的声誉点。

6 个答案:

答案 0 :(得分:0)

如果您将动作标记保留为undefined,则会使用当前位置。

somefile.php:

<form method="post">

将执行相同的uri

答案 1 :(得分:0)

action="<?php echo $_SERVER["PHP_SELF"];?>"替换为action=""

答案 2 :(得分:0)

删除表单中的操作标记。当前页面不强制执行bcoz操作。 如果给你$ _SERVER ['PHP_SELF']。结合你的全部道路。

答案 3 :(得分:0)

试试这个,这应该有效

<?php echo "<form method='post' action='".$_SERVER["PHP_SELF"]."' >";

答案 4 :(得分:0)

action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" 

尝试这一个这是w3school上发布的方法,指的是到现在为止它没有遇到任何问题。

答案 5 :(得分:0)

您没有处理可以尝试的数据

<!DOCTYPE html>
<html>
<head>
    <title>PHP Test</title>
</head>
<body>
<?php
    // define variables and set to empty values
    $name = $email = $gender = $comment = $website = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
        $name = test_input($_POST["name"]);
        $email = test_input($_POST["email"]);
        $website = test_input($_POST["website"]);
        $comment = test_input($_POST["comment"]);
        $gender = test_input($_POST["gender"]);
    }

    function test_input($data)
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
?>
    <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">

        Name: <input type="text" name="name"><br>
        E-mail: <input type="text" name="email"><br>
        Website: <input type="text" name="website"><br>
        Comment: <textarea name="comment" rows="5" cols="40"></textarea><br>

        Gender:
        <input type="radio" name="gender" value="female">Female
        <input type="radio" name="gender" value="male">Male<br>
        <input type="submit">
    </form>
    Name: <?php echo $name?> <br />
      Email: <?php echo $email?> <br />
      Gender: <?php echo  $gender?> <br />
      Comment: <?php echo  $comment?> <br />
      Website: <?php echo  $website?> <br />
</body>
</html>