繁体   English   中英

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

[英]PHP script not running inside a form tag

我最近安装了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>

当我访问该网站时,在输入字段名称之前,我会得到一个引号和一个大于号:“>名称。提交表单时,URL显示为

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

我无法输入URL的确切内容,因为该网站不允许我访问,但%3C实际上显示为小于号。 %20显示为空格。 因此,问题在于action标签内的php脚本无法运行。 然后用php脚本而不是当前页面的位置填充action变量。 为什么PHP脚本未在我的表单标签内运行?

解决方案:谢谢大家的帮助。 Abhik Chakraborty的意见使我找到了解决方案。 不,我没有使用.php扩展名保存文件。 我将扩展名更改为.php,它可以完美运行。 我本可以将其发布为解决方案,但由于我没有足够的声望点,我不得不等待八个小时。

如果未定义动作标签,它将使用当前位置。

somefile.php:

<form method="post">

将执行相同的uri

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

在您的表单中删除操作标签。 bcoz操作对于当前页面不是必需的。 如果你给了$ _SERVER ['PHP_SELF']。 彰显您的完整道路。

试试这个,这应该工作

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

试试这个,这是w3school上发布的指向自己页面的方法,到目前为止,它没有遇到任何问题。

您没有处理数据,可以尝试一下

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM