我正在尝试创建一个 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 lang="en">
<head>
<title>Deliveries</title>
</head>
<body class="container">
<form method="post" action="">
<input type="text" name="truck_name" Placeholder="Truck Name" value="" required>
<input type="number" name="max_weight" Placeholder="Max weight in lbs" value="" required>
<h4>Delivery Locations</h4>
<input type="text" name="location_1" Placeholder="Location 1" value="" required>
<input type="number" name="package_1" Placeholder="Package weight in lbs" value="" required>
<input type="text" name="location_1" Placeholder="Location 2" value="">
<input type="number" name="package_2" Placeholder="Package weight in lbs" value="" required>
<input type="text" name="location_1" Placeholder="Location 3" value="" required>
<input type="number" name="package_3" Placeholder="Package weight in lbs" value="" required>
<input type="submit" name="submit" value="Get Trips">
<input type="reset" name="reset" value="Reset">
</form>
<?php
if (isset($_POST['submit']))
{ ?>
<h2><?php echo "Trip Routes"; ?></h2>
<?php
$truck_1->print_truck();
trip_1();
trip_2();
}
?>
</body>
</html>
这是预期的结果:
行程 1:地点 2、地点 3
行程 2:位置 1