【问题标题】:On form submission the browser loads the php file that had the code for handling the form在提交表单时,浏览器会加载包含处理表单代码的 php 文件
【发布时间】:2016-05-19 17:16:13
【问题描述】:

我正在编写我的第一个 php 代码...我正在尝试提交表单。
我有两个 php 文件。 index.php(包含表单)和 process.php(包含处理表单的方法)。
但是在提交表单时,浏览器会转到 process.php,所以什么也没有显示。 我正在尝试在 index.php 中回显结果。

请记住,这是我的第一个 php 代码... :-)

这是 index.php

<!DOCTYPE html>
<html>
<?php
  include 'process.php';
  $newletter1 = new newsletter();
?>
  <head>
    <meta charset="utf-8">
  <title></title>
  </head>
  <body>
    <form action="process.php" method="post">
      <input type="text" name="email" placeholder="Your Email Address..."><br><br>
      <input type="submit">
    </form>
    <h4><?php $newletter1 -> abc(); ?></h4>
  </body>
  </html>

这是 process.php

class newsletter
{

  public function abc()
  {
    if (isset($_POST["email"])) {
      $input = $_POST["email"];
      if (empty($input)) {
        echo "Please provide an email address!";
      }else{
        echo "Thanks for subscribing " . $input;
      }
    }else{
      echo "ELSE is running...";
    }
  }

}

【问题讨论】:

  • process.php 是一个类定义。除非你在某个地方实例化它,否则它不会运行。因此,您需要将&lt;form action="process.php" 更改为&lt;form action="" 以使其启动index.php,这是您包含process.php的位置
  • @RiggsFolly 我更喜欢你的解释。
  • 非常感谢...它开始工作了。你的意思是如果 process.php 不是一个类定义我可以写 `
  • 如果只是普通的 PHP 代码,是的
  • 我明白了...非常感谢您的帮助。 :-) @RiggsFolly

标签: php forms


【解决方案1】:

您的脚本process.php 只是一个类定义。

除非实例化并调用方法,否则类什么都不做。

当您将它包含在您的 index.php 中并将其实例化时,我建议您更改您的 &lt;form&gt; 标记以使其自行运行,而留下 href="" 即可。

<?php
  // run this only if we are being set info by the user
  // so not when the form is first loaded.
  $to_show_later = '';
  if ($_SERVER['REQUEST_METHOD'] == 'POST' ) {
      include 'process.php';
      $newletter1 = new newsletter();
      $to_show_later = $newsletter1->abc();
  }
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
    <form action="" method="post">
      <input type="text" name="email" placeholder="Your Email Address..."><br><br>
      <input type="submit">
    </form>
    <h4><?php echo $to_show_later; ?></h4>
</body>
</html>

直接从类方法中回显也是不好的做法,因此请更改此设置以返回数据。

class newsletter
{

  public function abc()
  {
    $reply = '';
    if (isset($_POST["email"])) {
      $input = $_POST["email"];
      if (empty($input)) {
        $reply = "Please provide an email address!";
      }else{
        $reply = "Thanks for subscribing " . $input;
      }
    }else{
      $reply = "ELSE is running...";
    }
    return $reply;
  }

}

【讨论】:

  • 现在,就是我们所说的“答案”,女士们和男士们。
【解决方案2】:

在您的 process.php 中将其替换为:

if (isset($_POST["email"])) {
      $input = $_POST["email"];
      if (empty($input)) {
        echo "Please provide an email address!";
      }else{
        echo "Thanks for subscribing " . $input;
      }
    }else{
      echo "ELSE is running...";
    }

它会起作用,您不必包含 process.php 来仅提交表单

【讨论】:

  • 您作为代码发布的内容与 OP 在条件句中使用的内容没有区别。另外,Riggs 在 cmets 中解释了为什么它对他们不起作用。
  • 尝试并测试代码。我删除了课程部分。这样做 $_POST 是为了提供发布的数据。如果你有其他解释可以给@Fred
  • 您编写并声明的代码块 “在您的 process.php 中将其替换为:” 是不正确的。那是他们正在使用的确切代码。只有您声明 “它将起作用,并且您不必仅包含 process.php 才能提交表单” 的最后一部分在此处有效,并且应该已发布为 “将其更改为: &lt;form action="" method="post"&gt;"。我们需要在此处“点 i 并交叉 t”。这为未来的读者提供问题/答案。
  • 那直接提交到process.php呢?
猜你喜欢
  • 1970-01-01
  • 2013-05-08
  • 1970-01-01
  • 1970-01-01
  • 2011-08-22
  • 2017-03-18
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
相关资源
最近更新 更多