【问题标题】:Error while Processing a Basic Form in PHP在 PHP 中处理基本表单时出错
【发布时间】:2012-05-20 18:28:48
【问题描述】:

问题如下:

文件 1 的名称为:Lesson 17_Forms - Simple Form.php
和 文件 2 的名称为:Lesson 17_Forms - Process.php

文件1的内容如下:

<html>
    <title>Lesson 17_Forms - Simple Form</title>
    <head>
        <center><h1>Lesson 17_Forms - Simple Form</h1></center>
    </head>

    <body>
    <form action= "Lesson 17_Forms - Process.php" method="post">
        Username: <input type="text" name="username" value="" />
        <br />
        Password: <input type="password" name="password" value="" />
        <br />
        <input type="submit" name="submit" value="Submit" />
    </form>
    </body>
</html>

文件2的内容:

<html>
    <title>Lesson 17_Forms - Process</title>
    <head>
        <center><h1>Lesson 17_Forms - Process</h1></center>
    </head>

    <body>
        <?php
            // Ultra-simple form processing
            // Just retrieve the value and return it to the browser

            $username = $_POST['username'];
            $password = $_POST['password'];

            echo "{$username}: {$password}";
        ?>
    </body>
</html>

两个文件都在同一个目录中,当我尝试通过单击提交按钮来处理表单时,我收到以下错误:

找不到对象!

在此服务器上找不到请求的 URL。上的链接 引用页面似乎是错误的或过时的。请告知作者 关于错误的那个页面。

如果您认为这是服务器错误,请联系网站管理员。

错误 404

localhost 2012 年 5 月 12 日星期六 02:40:39 PM EEST Apache/2.2.21 (Unix) DAV/2 mod_ssl/2.2.21 OpenSSL/1.0.0c PHP/5.3.8 mod_apreq2-20090110/2.7.1 mod_perl/2.0.5 Perl/v5.10.1

非常感谢任何建议。

【问题讨论】:

  • 两个文件在同一个目录吗?
  • 另外,head 标签上不应有任何“可见”元素。您的标题 (&lt;h1&gt;) 应该进入正文。此外,&lt;center&gt; 不情愿。使用 CSS。
  • 如果文件名中不使用空格怎么办?
  • @Truth:他真的什么都做不了,而你现在让他承受着他在不久的将来无法理解的事情
  • @CompilingCyborg: 你提交表单后看到了什么url?

标签: php forms post


【解决方案1】:

我将使用您现有的代码并对其进行修改。使用新文件名。 我也更正了您的 HTML。

创建一个名为:form.php的文件,插入以下代码:

<html>
    <head>
        <title>Submit form</title>
    </head>

    <body>
        <center><h1>Submit form</h1></center>

        <form action="process.php" method="post">
            Username:<input type="text" name="username" value="" />
            <br />
            Password: <input type="password" name="password" value="" />
            <br />
            <input type="submit" name="submit" value="Submit" />
        </form>
    </body>
</html>

创建一个名为:process.php的文件,插入以下代码:

<html>
    <head>
        <title>Your form</title>
    </head>

    <body>
        <center><h1>Lesson 17_Forms - Process</h1></center>
        <?php
            // Ultra-simple form processing
            // Just retrieve the value and return it to the browser

            $username = $_POST['username'];
            $password = $_POST['password'];

            echo "{$username}: {$password}";
        ?>
    </body>
</html>

应该可以完美运行,记住将两个文件放在同一个文件夹中。

【讨论】:

  • 我不知道为什么
【解决方案2】:

确保两个文件都在同一个目录中。此外,消除文件名中的任何空格,虽然有效,但它们会引起巨大的头痛。因此,将您的文件命名为:lesson17_forms_process.php

另外,经过一些修改,这是我得到的代码:

<?php

//Initialize variables
$fields_set = false;
$username = "";
$password = "";

//Validity check
if (isset($_POST['username']) && isset($_POST['password'])) {
    $fields_set = true;
    $username = $_POST["username"];
    $password = $_POST["password"];
}

?>
<html>
<head>
    <title>Lesson 17_Forms - Process</title>
</head>

<body>
<h1 style="text-align: center;">Lesson 17_Forms - Process</h1>

<?php

//Display results if validity passed, or error in case it didn't.
if ($fields_set) {
    echo "{$username}: {$password}";
}
else {
    echo "Error! username or password not set!";
}
?>
</body>
</html>

几点

  • 在开始发送 HTML 之前,我会处理所有表单数据。尽可能分离逻辑和显示。
  • 我从head 中删除了h1 元素,并将其放在body(它所属的位置)中。所有页面内容必须进入body
  • 我删除了演示元素 &lt;center&gt; 并将其替换为 CSS。
  • 我在处理之前验证输入。
  • 如果表单无效,我会显示错误。

【讨论】:

  • 我建议您将示例重写为更健壮的:“首先检索和处理数据 - 在完成所有输出之后”
  • @zerkms:我很清楚,如果由我决定,这个例子将是完美的 MVC 模式,但我不想将 OP 与额外的细节混淆。一次一个障碍。
  • "但我不想将 OP 与额外的细节混淆" --- "另外,head 标签上不应有任何“可见”元素。您的标题 (

    ) 应该进入正文。另外,
    不情愿。使用 CSS。” --- 你今天很稳定 ;-)

  • 非常感谢您的 cmets。谢谢
  • @Truth: 顺便说一句,对不起,不想成为那个$$,+1
【解决方案3】:

url 中不允许有空格 -> 将名称更改为 simple_form.php 和 process.php。

或者,如果您想在名称中保留空格,则在将其用作 url 时将所有空格替换为 '%20'。

或者最好的选择是使用 urlencode -> 它会自动将所有不允许的字符替换为其网络可接受的占位符。

<form action=<?php echo '"'.urlencode("Lesson 17_Forms - Process.php").'"'; ?> method="post">

【讨论】:

  • 在处理症状的同时。基础问题依然存在。你不应该通过坏代码破解你的方式,你需要修复它..
  • 是的,在 url 名称中保留空格是一种不好的做法。但是 urlencode 的使用是不可避免的,如果他不得不使用一些带有一些数据的外部 url(可能包含一些空格等),则必须使用 urlencode。例如-> www.externalurl.com?data=abc%20bac%mma -> 这就是为什么我还为他提供了一个 urlencode 来解决非法字符问题。
  • 表单操作很少(也不应该)包含 get 变量。通常没有理由在表单操作中使用空格。既然你提到了,那就太好了。你得到我的 +1 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多