简体   繁体   中英

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. I need to create a file named resultTemp.php as an event handler for my html form. So I have to create a function named getTemp() in the php file which receive the parameter month, and region. This function will check the month, and region. The function is called from resultTemp.php where two parameters(month and region) are passed.

Below is my code in 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.

I think you meant to use or ( || ) instead of 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.

Your if sections are saying "if the month is both 12, 1 and 2 at the same time" which is never true. Try changing the AND operator (&&) to a OR operator (||)

Eg:

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

Here you 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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