Skip to content
Advertisement

Input from form to url?

So the goal is to take the input from a form append it to the end of the URL and then return the HTML from that page. I am not entirely sure how to take the forms value (in this case $2) and attach it to the URL.

<html>
    <head>
        <title>Metar Test</title>
    </head>
    <body>
            <form action="" method="POST">
                <p>IACO Code: <input type="text" name="$2" value=""></p>
                <input type="submit" name="submit" value="Submit">
            </form>
        <?php $html=file_get_contents("http://www.metar.mysite.net/metar?id=$2")?>
        <?php echo $_POST ["$html"];?>
   </body>
</html>

Advertisement

Answer

On submit any input from a form with method=”POST” will be stored in the $_POST global. You can access it from there and append it to your URL string.

<html>
    <head>
        <title>Metar Test</title>
    </head>
    <body>
            <form action="" method="POST">
                <p>IACO Code: <input type="text" name="iacoCode" value=""></p>
                <input type="submit" name="submit" value="Submit">
            </form>
        <?php 
        if (isset($_POST["iacoCode"])) {
          $html = file_get_contents("http://www.metar.mysite.net/metar?id=" . $_POST["iacoCode"]);

          echo $html;
        }
        ?>
   </body>
</html> 

Using the IF statement to check if it is set will prevent it from loading the URL with no variable.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement