Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何基于表单字段输入值创建PHP输出?_Php - Fatal编程技术网

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

如何基于表单字段输入值创建PHP输出?,php,Php,我正在尝试创建一个php脚本,它从表单中获取输入值,然后在提交表单时输出推荐的旅行路线。我将form方法设置为POST。问题是我得到了一个空白输出 我不确定问题是我的代码还是表单POST方法 class Truck { // constructor public function __construct($truck_name, $max_weight) { $this->truck_name = $truck_name; $this-&g

我正在尝试创建一个php脚本,它从表单中获取输入值,然后在提交表单时输出推荐的旅行路线。我将form方法设置为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

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

正如我在一篇评论中提到的,随后被删除并被指出——上面的代码没有多大意义。我很感激您正在学习PHP,但是类对象实例化和赋值的方式是不正确的。在类方法中调用类构造函数是一种从头到脚的操作。应该使用所需的任何参数实例化该类,然后调用它的方法。一个类可以作为另一个类的参数给出——这类似于我在下面所做的,我创建了一个名为Trip的新类,并分配了一个truck和location的实例。。。。我可能没有抓住要点,但希望能有所帮助

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

在ifisset$_POST['submit']的另一面放置一个else条件,通过这样做,您可以知道是否有成功的POST请求。我没有编辑问题的选项。我还可以如何共享表单html?您可以编辑问题,仔细查看,您会发现链接您的表单是否有action@PraveenPanishetti我添加了else条件,仍然得到空白输出。谢谢。这肯定给了我一个想法,我需要做什么来获得预期的产出。我将很快更新最终结果。再次感谢!我想在一次行程中显示2个位置。比如$trip_1=新trip$truck、$loc_1、$loc_2$跳闸1->显示;我怎样才能用你的代码设置做到这一点?它肯定给了我一个想法,我需要做些什么来获得预期的输出。。。真正地您是否尝试过应用上述任何一项,顺便说一句,这些内容并非旨在成为实际的代码,而是更多的建议,如果是的话,它在哪里?原来的代码是如此的有缺陷,以致于它毫无价值。也许,作为一个建议,您可以提供数组/对象中的许多位置作为Trip类的第二个参数,并处理它??!?我很清楚你的代码是一个建议。我使用它作为正确实例化函数的指南。下面是我如何使用magic\uuuuu set和\uuuuu get方法设置的。Trip类:类Trip{private$truck;private$location;公共函数{u构造truck$truck,$locs=array{$this->truck=$truck;$this->location=$locs;}}。初始化:$trip_1=新Triparraylocation1=>$loc_1$跳闸1->位置1;我上一个问题措辞不好。我只是想看看我把数组进程弄错了什么地方。对不起,如果最后一条评论听起来有点烦躁-我是。不管怎么说,上面修改过——也许会有帮助,但我可能又错过了重点。
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:
-------------------------