duananyantan04633 2016-05-19 17:16
浏览 49
已采纳

在表单提交时,浏览器加载具有处理表单代码的php文件

I'm writing my first php code... I'm trying to submit a form.
I have two php files. index.php(which contains the form) and process.php(which contains the method that handles the form).
But on form submission, the browser heads to process.php, so nothing is displayed. I'm trying to echo the result in index.php .

And keep in mind this is my very first php code... :-)

This is 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>

And this is 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...";
    }
  }

}
  • 写回答

2条回答 默认 最新

  • dongqie4402 2016-05-19 18:07
    关注

    Your script process.php is just a class definition.

    A class does nothing unless it is instantiated and a method called.

    As you are including it and instantiating it in your index.php I would suggest changing your <form> tag so it runs itself, leaving href="" will do that.

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

    It's also bad practice to echo directly out of a class method, so change this so it return the data.

    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;
      }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题