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

我正在尝试创建一个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


料青山看我应如是
浏览 178回答 2
2回答

子衿沉夜

在尝试调用方法之前,不要实例化任何类。您需要实例化该类,例如$ truck = new truck();。然后从那里调用您的方法。
打开App,查看更多内容
随时随地看视频慕课网APP