簡體   English   中英

PHP:將后值傳遞到 function

[英]PHP: passing post value into function

問題

這個問題需要我編寫一個 PHP 代碼,告訴用戶基於他們在 html 表格中選擇的區域和月份的溫度。 我需要創建一個名為 resultTemp.php 的文件作為我的 html 表單的事件處理程序。 所以我必須在 php 文件中創建一個名為 getTemp() 的 function,它接收參數月份和區域。 這個 function 將檢查月份和地區。 function 從 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 

代碼運行沒有錯誤,但 function 回顯未顯示在瀏覽器中,知道我做錯了什么提前謝謝。

我認為您的意思是使用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沒有任何意義。

您的 if 部分說“如果月份同時是 12、1 和 2”,這絕不是真的。 嘗試將 AND 運算符 (&&) 更改為 OR 運算符 (||)

例如:

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

這里是 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"]);
?>

您使用的是&&而不是|| . 我還重新格式化了代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM