简体   繁体   English

PHP:将后值传递到 function

[英]PHP: passing post value into function

QUESTION问题

This question requires me to write a PHP code that tells user the temperature based on the region and month they choose in html form.这个问题需要我编写一个 PHP 代码,告诉用户基于他们在 html 表格中选择的区域和月份的温度。 I need to create a file named resultTemp.php as an event handler for my html form.我需要创建一个名为 resultTemp.php 的文件作为我的 html 表单的事件处理程序。 So I have to create a function named getTemp() in the php file which receive the parameter month, and region.所以我必须在 php 文件中创建一个名为 getTemp() 的 function,它接收参数月份和区域。 This function will check the month, and region.这个 function 将检查月份和地区。 The function is called from resultTemp.php where two parameters(month and region) are passed. function 从 resultTemp.php 调用,其中传递了两个参数(月份和地区)。

Below is my code in resultTemp.php下面是我在 resultTemp.php 中的代码

//check post value
if (strlen($_POST["month"]) > 0) {
  $month = $_POST["month"];
} else {
  echo "select the month you desire!";
}

if (strlen($_POST["region"]) > 0) {
  $region = $_POST["region"];
} else {
  echo "select the region you wanna visit!";
}

echo "Month: " . $month . "<br>";
echo "Month: ". $region . "<br>";




//function declaration

function getTemp($region, $month) {
  if ($region == "North") {
    if ($month == "12" && $month == "1" && $month == "2") {
      echo "It is winter. Too cold. Wear thick clothes.";
    }
    elseif($month == "3" && $month == "4" && $month == "5") {
      echo "It is spring. Nice temperature. Jacket is enough.";
    }
    elseif($month == "6" && $month == "7" && $month == "8") {
      echo "It is summer. Hot.";
    }
    elseif($month == "9" && $month == "10" && $month == "11") {
      echo "It is autumn. Nice temperature. Jacket is enough.";
    }
  }

  elseif($region == "South") {
    if ($month == "6" && $month == "7" && $month == "8") {
      echo "It is winter. Too cold. Wear thick clothes.";
    }
    elseif($month == "9" && $month == "10" && $month == "11") {
      echo "It is spring. Nice temperature. Jacket is enough.";
    }
    elseif($month == "12" && $month == "1" && $month == "2") {
      echo "It is summer. Hot.";
    }
    elseif($month == "3" && $month == "4" && $month == "5") {
      echo "It is autumn. Nice temperature. Jacket is enough.";
    }
  } else {
    echo "<br>".
    "undefined parameter";
  }
}

echo "<br>";
getTemp($_POST["region"], $_POST["month"]);//call function 

The code runs without an error, but the function echo is not displaying in the browser, any idea what I did wrong thanks in advance.代码运行没有错误,但 function 回显未显示在浏览器中,知道我做错了什么提前谢谢。

I think you meant to use or ( || ) instead of and ( && ):我认为您的意思是使用or ( || ) 而不是and ( && ):

function getTemp($region, $month) {
  if ($region == "North") {
    if ($month == "12" || $month == "1" || $month == "2") {
      echo "It is winter. Too cold. Wear thick clothes.";
    } elseif($month == "3" || $month == "4" || $month == "5") {
      echo "It is spring. Nice temperature. Jacket is enough.";
    } elseif($month == "6" || $month == "7" || $month == "8") {
      echo "It is summer. Hot.";
    } elseif($month == "9" || $month == "10" || $month == "11") {
      echo "It is autumn. Nice temperature. Jacket is enough.";
    }
  } elseif($region == "South") {
    if ($month == "6" || $month == "7" || $month == "8") {
      echo "It is winter. Too cold. Wear thick clothes.";
    } elseif($month == "9" || $month == "10" || $month == "11") {
      echo "It is spring. Nice temperature. Jacket is enough.";
    } elseif($month == "12" || $month == "1" || $month == "2") {
      echo "It is summer. Hot.";
    } elseif($month == "3" || $month == "4" || $month == "5") {
      echo "It is autumn. Nice temperature. Jacket is enough.";
    }
  } else {
    echo "<br>".
    "undefined parameter";
  }
}

$month == 6 && $month == 7 && $month == 8 doesn't make any sense. $month == 6 && $month == 7 && $month == 8没有任何意义。

Your if sections are saying "if the month is both 12, 1 and 2 at the same time" which is never true.您的 if 部分说“如果月份同时是 12、1 和 2”,这绝不是真的。 Try changing the AND operator (&&) to a OR operator (||)尝试将 AND 运算符 (&&) 更改为 OR 运算符 (||)

Eg:例如:

if ($month == "12" || $month == "1" || $month == "2") {
  echo "It is winter. Too cold. Wear thick clothes.";
}

Here you go:这里是 go:

<?php
if(strlen($_POST["month"]) > 0){
  $month = $_POST["month"];
}
else{
  echo "select the month you desire!";
}

if(strlen($_POST["region"]) > 0){
  $region = $_POST["region"];
}
else{
  echo "select the region you wanna visit!";
}

echo "Month: ".$month."<br>";
echo "Month: ".$region."<br>";

//call function

function getTemp($region, $month){
  if($region == "North"){
    if($month == "12" || $month == "1" || $month == "2"){
      echo "It is winter. Too cold. Wear thick clothes.";
    }
    elseif($month == "3" || $month == "4" || $month == "5"){
      echo "It is spring. Nice temperature. Jacket is enough.";
    }
    elseif($month == "6" || $month == "7" || $month == "8"){
      echo "It is summer. Hot.";
    }
    elseif($month == "9" || $month == "10" || $month == "11"){
      echo "It is autumn. Nice temperature. Jacket is enough.";
    }
  }
    
  elseif($region == "South"){
    if($month == "6" || $month == "7" || $month == "8"){
      echo "It is winter. Too cold. Wear thick clothes.";
    }
    elseif($month == "9" || $month == "10" || $month == "11"){
      echo "It is spring. Nice temperature. Jacket is enough.";
    }
    elseif($month == "12" || $month == "1" || $month == "2"){
      echo "It is summer. Hot.";
    }
    elseif($month == "3" || $month == "4" || $month == "5"){
      echo "It is autumn. Nice temperature. Jacket is enough.";
    }
  }
  else{
    echo "<br>"."undefined parameter";
  }
}
echo "<br>";
getTemp($_POST["region"], $_POST["month"]);
?>

You were using the && instead of the ||您使用的是&&而不是|| . . I also reformatted the code.我还重新格式化了代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM