Abort Mission
Abort Mission

Reputation: 13

Is there a reason why my php code won't run with the HTML?

I'm practicing php right now on a simple web form problem. Long story short, I just need to create a very basic web form that takes in a two-dimensional array of European cities which show the distance in km of how far away it is from other European cities. All I need to do is create a web form where you can input any city found in that two-dim array and have it calculate the distance between the Start city and the End city (i.e Berlin is located blah miles away from Prague after inputting "Berlin" and "Prague" in the text field.

I think I have the problem almost solved but the issue is that phpstorm is telling me that certain variables are undefined even though they were already defined in my function and as a variable before that function. I apologize if this problem seems trivial but I'm just learning to digest php at the moment. Here is my code below. I included the html and the php code in one page.

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

Upvotes: 0

Views: 135

Answers (3)

Amaresh S M
Amaresh S M

Reputation: 3010

your php code executes first so your getting some error. just add

if(isset($_POST['submit'])){
...
}

in your php code, so that your php code will only executed after submitting your form details.

$City1 = $_POST['firstCity'];
$City2 = $_POST['secondCity'];

The above 2 variables are not initialized they are getting values from the form, so form data needs to be submitted before executing the php code. And you should call the function to calculate the distance.

function calling:

if(isset($_POST['submit']))
{
$KMtoMiles = 0.62;
$City1 = $_POST['firstCity'];
$City2 = $_POST['secondCity'];
euDis(trim($City1),trim($City2));  //function calling
}

like this:

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

Upvotes: 0

Cotur
Cotur

Reputation: 450

I have changed following things.

  1. You did not call your function.
  2. The cities names are keys of your array. in_array returns true if your variable is member of array, not key.
<?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>

Upvotes: 1

Gaurav
Gaurav

Reputation: 442

There are two issues in code.first is before assign post variable check if data is available. The second one is the city available in distances array.

Fixed your issue. Try this code once

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

Upvotes: 0

Related Questions