[英]Is there a reason why my php code won't run with the HTML?
我现在正在通过一个简单的Web表单问题练习php。 长话短说,我只需要创建一个非常基本的Web表单即可使用二维数组的欧洲城市,这些表单以公里为单位显示距其他欧洲城市的距离。 我需要做的就是创建一个网络表单,您可以在其中输入在两维数组中找到的任何城市,并计算起点城市和终点城市之间的距离(即输入“文本字段中的“柏林”和“布拉格”。
我认为我的问题已基本解决,但问题是phpstorm告诉我某些变量是未定义的,即使它们已在我的函数中定义且在该函数之前已定义为变量。 如果这个问题看起来微不足道,我深表歉意,但是我现在正在学习摘要php。 这是下面的代码。 我在一页中包含了html和php代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Distance between European Cities</title>
</head>
<body>
<h1>Distance between European Cities</h1>
<h2>Enter valid European cities from this list: Berlin, Moscow, Paris, Prague, Rome</h2>
<?php
$Distances = array(
"Berlin" => array("Berlin" => 0, "Moscow" => 1607.99, "Paris" => 876.96, "Prague" => 280.34, "Rome" => 1181.67),
"Moscow" => array("Berlin" => 1607.99, "Moscow" => 0, "Paris" => 2484.92, "Prague" => 1664.04, "Rome" => 2374.26),
"Paris" => array("Berlin" => 876.96, "Moscow" => 641.34, "Paris" => 0, "Prague" => 885.38, "Rome" => 1105.76),
"Prague" => array("Berlin" => 280.34, "Moscow" => 1607.99, "Paris" => 885.38, "Prague" => 0, "Rome" => 922),
"Rome" => array("Berlin" => 1181.67, "Moscow" => 2374.26, "Paris" => 1105.76, "Prague" => 922, "Rome" => 0));
$KMtoMiles = 0.62;
$City1 = $_POST['firstCity'];
$City2 = $_POST['secondCity'];
function euDis($City1, $City2, $Distances){
if (empty($City1) || empty($City2)) {
echo "Please input two cities in the required fields.<br />\n";
} elseif (in_array($City1, $Distances) == FALSE || in_array($City2, $Distances) == FALSE) {
echo "You inputted one or more cities that are not in our list.<br />";
} else {
if (isset($_POST['submit'])) {
$City1 = stripslashes($_POST['firstCity']);
$City2 = stripslashes($_POST['secondCity']);
if (isset($Distances[$City1][$City2])) {
echo "<p>The distance from $City1 to $City2 is " . $Distances[$City1][$City2] . " kilometers or " . round((0.62 * $Distances[$City1][$City2]), 2) . " miles.</p>\n";
} else {
echo "<p>$City1 and $City2 are not in this list.</p>\n";
}
}
}
}
?>
<form action= "eudistance.php" method="post">
City 1: <input type="text" name="firstCity" /><br />
City 2: <input type="text" name="secondCity" /><br />
<input type="reset" value="Clear All" />
<input type="submit" name="submit" value="Enter" />
</form>
</body>
</html>
我改变了以下事情。
keys
。 如果您的变量是数组的成员,而不是键,则in_array
返回true
。 <?php
function euDis($City1, $City2){
$Distances = array(
"Berlin" => array("Berlin" => 0, "Moscow" => 1607.99, "Paris" => 876.96, "Prague" => 280.34, "Rome" => 1181.67),
"Moscow" => array("Berlin" => 1607.99, "Moscow" => 0, "Paris" => 2484.92, "Prague" => 1664.04, "Rome" => 2374.26),
"Paris" => array("Berlin" => 876.96, "Moscow" => 641.34, "Paris" => 0, "Prague" => 885.38, "Rome" => 1105.76),
"Prague" => array("Berlin" => 280.34, "Moscow" => 1607.99, "Paris" => 885.38, "Prague" => 0, "Rome" => 922),
"Rome" => array("Berlin" => 1181.67, "Moscow" => 2374.26, "Paris" => 1105.76, "Prague" => 922, "Rome" => 0));
if (empty(trim($City1)) || empty(trim($City2))) {
echo "Please input two cities in the required fields.<br />";
} elseif ($Distances[$City1] == null || $Distances[$City2] == null) {
echo "You inputted one or more cities that are not in our list.<br />";
} else {
if (isset($Distances[$City1][$City2])) {
echo "<p>The distance from $City1 to $City2 is " . $Distances[$City1][$City2] . " kilometers or " . round((0.62 * $Distances[$City1][$City2]), 2) . " miles.</p>";
} else {
echo "<p>$City1 and $City2 are not in this list.</p>";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Distance between European Cities</title>
</head>
<body>
<h1>Distance between European Cities</h1>
<h2>Enter valid European cities from this list: Berlin, Moscow, Paris, Prague, Rome</h2>
<?php
if($_POST)
{
euDis($_POST['firstCity'], $_POST['secondCity']);
}
?>
<form action= "" method="post">
City 1: <input type="text" name="firstCity" /><br />
City 2: <input type="text" name="secondCity" /><br />
<input type="reset" value="Clear All" />
<input type="submit" name="submit" value="Enter" />
</form>
</body>
</html>
您的php代码首先执行,因此您遇到了一些错误。 只需添加
if(isset($_POST['submit'])){
...
}
在您的php代码中,这样您的php代码将仅在提交表单详细信息后执行。
$City1 = $_POST['firstCity'];
$City2 = $_POST['secondCity'];
上面的两个变量没有初始化,它们是从表单获取值,因此在执行php代码之前需要提交表单数据。 并且您应该调用该函数来计算距离。
函数调用:
if(isset($_POST['submit']))
{
$KMtoMiles = 0.62;
$City1 = $_POST['firstCity'];
$City2 = $_POST['secondCity'];
euDis(trim($City1),trim($City2)); //function calling
}
像这样:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Distance between European Cities</title>
</head>
<body>
<h1>Distance between European Cities</h1>
<h2>Enter valid European cities from this list: Berlin, Moscow, Paris, Prague, Rome</h2>
<?php
if(isset($_POST['submit']))
{
$KMtoMiles = 0.62;
$City1 = $_POST['firstCity'];
$City2 = $_POST['secondCity'];
euDis(trim($City1),trim($City2));
}
function euDis($City1, $City2){
$Distances = array(
"Berlin" => array("Berlin" => 0, "Moscow" => 1607.99, "Paris" => 876.96, "Prague" => 280.34, "Rome" => 1181.67),
"Moscow" => array("Berlin" => 1607.99, "Moscow" => 0, "Paris" => 2484.92, "Prague" => 1664.04, "Rome" => 2374.26),
"Paris" => array("Berlin" => 876.96, "Moscow" => 641.34, "Paris" => 0, "Prague" => 885.38, "Rome" => 1105.76),
"Prague" => array("Berlin" => 280.34, "Moscow" => 1607.99, "Paris" => 885.38, "Prague" => 0, "Rome" => 922),
"Rome" => array("Berlin" => 1181.67, "Moscow" => 2374.26, "Paris" => 1105.76, "Prague" => 922, "Rome" => 0));
if (empty(trim($City1)) || empty(trim($City2))) {
echo "Please input two cities in the required fields.<br />\n";
}
else {
if (isset($Distances[$City1][$City2])) {
echo "<p>The distance from $City1 to $City2 is " . $Distances[$City1][$City2] . " kilometers or " . round((0.62 * $Distances[$City1][$City2]), 2) . " miles.</p>\n";}
else {
echo "<p>$City1 and $City2 are not in this list.</p>\n";
} }
}
?>
<form action= "" method="post">
City 1: <input type="text" name="firstCity" /><br />
City 2: <input type="text" name="secondCity" /><br />
<input type="reset" value="Clear All" />
<input type="submit" name="submit" value="Enter" />
</form>
</body>
</html>
代码中有两个问题。第一个是在分配发布变量之前检查数据是否可用。 第二个是距离数组中可用的城市。
解决了您的问题。 尝试一次此代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Distance between European Cities</title>
</head>
<body>
<h1>Distance between European Cities</h1>
<h2>Enter valid European cities from this list: Berlin, Moscow, Paris, Prague, Rome</h2>
<?php
$Distances = array(
"Berlin" => array("Berlin" => 0, "Moscow" => 1607.99, "Paris" => 876.96, "Prague" => 280.34, "Rome" => 1181.67),
"Moscow" => array("Berlin" => 1607.99, "Moscow" => 0, "Paris" => 2484.92, "Prague" => 1664.04, "Rome" => 2374.26),
"Paris" => array("Berlin" => 876.96, "Moscow" => 641.34, "Paris" => 0, "Prague" => 885.38, "Rome" => 1105.76),
"Prague" => array("Berlin" => 280.34, "Moscow" => 1607.99, "Paris" => 885.38, "Prague" => 0, "Rome" => 922),
"Rome" => array("Berlin" => 1181.67, "Moscow" => 2374.26, "Paris" => 1105.76, "Prague" => 922, "Rome" => 0));
$KMtoMiles = 0.62;
if(isset($_POST['submit'])){
$City1 = $_POST['firstCity'];
$City2 = $_POST['secondCity'];
if (empty($City1) || empty($City2)) {
echo "Please input two cities in the required fields.<br />\n";
} elseif (is_array($Distances[$City1]) == False || is_array($Distances[$City2])== false ) {
echo "You inputted one or more cities that are not in our list.<br />";
} else {
if (isset($_POST['submit'])) {
$City1 = stripslashes($_POST['firstCity']);
$City2 = stripslashes($_POST['secondCity']);
if (isset($Distances[$City1][$City2])) {
echo "<p>The distance from $City1 to $City2 is " . $Distances[$City1][$City2] . " kilometers or " . round((0.62 * $Distances[$City1][$City2]), 2) . " miles.</p>\n";
} else {
echo "<p>$City1 and $City2 are not in this list.</p>\n";
}
}
}
}
?>
<form action= "" method="post">
City 1: <input type="text" name="firstCity" /><br />
City 2: <input type="text" name="secondCity" /><br />
<input type="reset" value="Clear All" />
<input type="submit" name="submit" value="Enter" />
</form>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.