Sunday, 5 July 2015

Call PHP Method with Parameter from HTML

 <?php
     if (isset($_POST['getData']) and isset($_POST['country']))
     {       
        $servername = "localhost";
        $username = "root";
        $password = "root";
        $dbname = "World";

        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);

        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
        $sql = "SELECT code, name FROM country where name='" .  $_POST['country'] . "';";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            // output data of each row
            while($row = $result->fetch_assoc()) {
                echo $row["code"]. " - " . $row["name"]. "<br>";
            }
        } else {
            echo "0 results";
        }
        $conn->close();
    }
?>


<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>

       <form action="" method="post">
           Country: <input type="text" name="country">
           <input type="submit" name ="getData" value="send"></input>
        </form>

    </body>
</html>

No comments:

SQL: Generate a range of numbers

SELECT ones.n + 10*tens.n + 100*hundreds.n + 1000*thousands.n FROM       (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) ones(n),      (VALU...