为了账号安全,请及时绑定邮箱和手机立即绑定

如何基于表单字段输入值创建PHP输出?

如何基于表单字段输入值创建PHP输出?

PHP
料青山看我应如是 2021-04-08 13:15:48
我正在尝试创建一个php脚本,该脚本从表单中获取输入值,然后在提交表单时输出建议的行程路线。我将表单方法设置为POST。问题是我得到空白输出。我不确定问题是我的代码还是POST方法。class Truck {    // constructor    public function __construct($truck_name, $max_weight) {        $this->truck_name = $truck_name;        $this->max_weight = $max_weight;    }       public function print_truck() {        if (isset($_POST['submit']))             {               $truck_1 = new Truck($truck_name, $max_weight);                  $this->truck_name = $_POST['truck_name'];                  $this->max_weight = $_POST['max_weight'];            }               echo $this->truck_name;    }} class Location {    // constructor    public function __construct($location_name, $location_weight) {        $this->location_name = $location_name;        $this->location_weight = $location_weight;    }    public function trip_1() {        if (isset($_POST['submit'])) {    $location_2 = new Location($location_name, $location_weight);        $this->location_name = $_POST['location_2'];        $this->location_weight = $_POST['package_2'];    $location_3 = new Location($location_name, $location_weight);        $this->location_name = $_POST['location_3'];        $this->location_weight = $_POST['package_3'];    }    echo "Trip #1 \n" . $this->location_2 . ", " . $this->location_3 . "\n";    }    public function trip_2() {    if (isset($_POST['submit'])) {    $location_1 = new Location($location_name, $location_weight);        $this->location_name = $_POST['location_1'];        $this->location_weight = $_POST['package_1'];    }    echo "Trip #2 \n" . $this->location_1;    }}# Here's the output HTML:?><!DOCTYPE HTML><html><head>  <title>Deliveries</title></head>这是预期的结果:行程1:位置2,位置3行程2:位置1
查看完整描述

2 回答

?
子衿沉夜

TA贡献1828条经验 获得超3个赞

在尝试调用方法之前,不要实例化任何类。您需要实例化该类,例如$ truck = new truck();。然后从那里调用您的方法。


查看完整回答
反对 回复 2021-04-23
  • 2 回答
  • 0 关注
  • 177 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信