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

时间:2019-04-14 07:21:03

标签: 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 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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

正如我在评论中提到(然后删除)并指出的那样-上面的代码实际上没有多大意义。感谢您正在学习PHP,但是实例化类对象和分配值的方式不正确。在类class constructor中调用method是很重要的事情。 class应该使用任何需要的参数实例化,然后调用methods。可以将一个类作为另一个类的参数,这与我在下面创建一个名为Trip的新类并分配trucklocation的实例的过程类似。 ...我可能已经错过了重点,但希望能有所帮助。

<?php

    class Truck {
        private $truck_name;
        private $truck_weight;

        public function __construct($name=false, $weight=false) {
            $this->truck_name = $name;
            $this->max_weight = $weight;
        }
        public function get_name(){
            return $this->truck_name;
        }
        public function get_max_weight(){
            return $this->max_weight;
        }
    }


    class Location {
        private $location_name;
        private $location_weight;

        public function __construct($name=false, $weight=false) {
            $this->location_name = $name;
            $this->location_weight = $weight;
        }
        public function get_location(){
            return $this->location_name;
        }
        public function get_location_weight(){
            return $this->location_weight;
        }
    }

    class Trip{
        private $truck;
        private $locations;

        public function __construct( Truck $truck, $locations=array() ){
            $this->truck=$truck;
            $this->locations=$locations;
        }
        public function display(){
            if( !empty( $this->locations ) ){

                $dots=str_repeat('-',25);

                printf(
                    "<h2>Trip Routes</h2>
                    <pre>%s\nTruck: %s\nMax-Weight: %s\n%s\n",
                    $dots,
                    $this->truck->get_name(),
                    $this->truck->get_max_weight(),
                    $dots
                );

                $total_locations_weight = 0;

                foreach( $this->locations as $index => $location ){
                    $total_locations_weight += floatval( $location->get_location_weight() );
                    printf( 
                        "[Drop: %d]\nLocation: %s\nLocation-Weight: %s\n\n",
                        $index+1,
                        $location->get_location(),
                        $location->get_location_weight()
                    );
                }
                printf("%s</pre>",$dots);

                /* Is there too much weight for the truck? */
                if( $total_locations_weight > $this->truck->get_max_weight() ){
                    printf(
                        "<pre><h1 style='color:red;margin:0;'>Maximum Weight Exceded</h1><div>Total weight is: %s</div><div>Limit exceded by: %s:</div>\n%s</pre>",
                        $total_locations_weight,
                        $total_locations_weight - $this->truck->get_max_weight(),
                        $dots
                    );
                }
            }
        }
    }

?>

<!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_2" placeholder="Location 2" value="">
        <input type="number" name="package_2" placeholder="Package weight in lbs" value="" required>

        <input type="text" name="location_3" 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( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['submit'] ) ){

            $truck_name = filter_input( INPUT_POST, 'truck_name', FILTER_SANITIZE_STRING );
            $max_weight = filter_input( INPUT_POST, 'max_weight', FILTER_SANITIZE_STRING );

            $location_1 = filter_input( INPUT_POST, 'location_1', FILTER_SANITIZE_STRING );
            $location_2 = filter_input( INPUT_POST, 'location_2', FILTER_SANITIZE_STRING );
            $location_3 = filter_input( INPUT_POST, 'location_3', FILTER_SANITIZE_STRING );

            $package_1 = filter_input( INPUT_POST, 'package_1', FILTER_SANITIZE_STRING );
            $package_2 = filter_input( INPUT_POST, 'package_2', FILTER_SANITIZE_STRING );
            $package_3 = filter_input( INPUT_POST, 'package_3', FILTER_SANITIZE_STRING );







            $truck=new Truck( $truck_name, $max_weight );

            $loc_1=new Location( $location_1, $package_1 );
            $loc_2=new Location( $location_2, $package_2 );
            $loc_3=new Location( $location_3, $package_3 );

            $trip=new Trip( $truck, array( $loc_1, $loc_2, $loc_3 ) );
            $trip->display();
        }
    ?>
    </body>
</html>

典型的输出可能是:

Trip Routes
-------------------------
Truck: The Crushinator
Max-Weight: 2500
-------------------------
[Drop: 1]
Location: London
Location-Weight: 1500

[Drop: 2]
Location: Glasgow
Location-Weight: 1500

[Drop: 3]
Location: Edinburgh
Location-Weight: 550

-------------------------
Maximum Weight Exceded
Total weight is: 3550
Limit exceded by: 1050:
-------------------------