Search code examples
phpformsgetisset

PHP_SELF on submit form


I have a question about using PHP_SELF on a submit from a form. This is what I have in my index.php:

<body>
    <div class="container">
        <div class="row">
            <div class="span12">
                <?php
                    // If you get an appid, 
                    if(isset($_GET['appid'])) {
                        // GET appid
                        $appid = $_GET['appid'];

                        $json_url  ='http://api.nameurl.com/api/gateway/call/1.4/getApp?appid=' . $appid;

                        $ch = curl_init($json_url);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

                        $str = curl_exec($ch);
                        curl_close($ch);

                        $data = json_decode($str);
                        $array = json_encode($data);
                    }  
                    else if(isset($_POST['submit'])){
                        $name = $_POST['appid'];
                        echo "User Has submitted the form and entered this name : <b> $name </b>";
                        echo "<br>You can use the following form again to enter a new name."; 
                    }
                    else{ ?>
                        <!-- Form -->
                        <form class="well" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">  
                            <label>Your appid</label>  
                            <input type="text" class="span3" name="appid" id="appid" placeholder="Type your appid here...">  <br>
                            <button type="submit" id="submit" class="btn btn-primary" data-loading-text="Loading...">Submit</button>  
                        </form>

                <?php }   
                ?>

                <p id="errors" class="text-error"></p>
            </div>
        </div>

        <hr>

    </div>

I check if I get an appid from the previous page.
Yes -> get data from api
No -> Show the form so you can insert an appid

Then when you press on submit on the form I want to get the appid insert and do the same operations as above.

What am I doing wrong?


Solution

  • You've got the method for the form set to post, but you're trying to access the appid using $_GET['appid']. You either need to change the form method to get, or access the appid using $_POST['appid']