提交表单上的PHP_SELF

时间:2013-05-23 09:17:54

标签: php forms get isset

我有一个关于在表单提交中使用PHP_SELF的问题。 这就是我在 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>

我检查是否从上一页获得了appid。
是的 - &gt;从api获取数据 不 - &gt;显示表单,以便插入appid

然后当您按下表单上的提交时,我想获取appid 插入并执行与上述相同的操作。

我做错了什么?

5 个答案:

答案 0 :(得分:3)

您已将表单设置为post的方法,但您尝试使用$_GET['appid']访问该appid。您需要将表单方法更改为get,或使用$_POST['appid']

访问appid

答案 1 :(得分:3)

试试这个......

<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" name="submit" class="btn btn-primary" value="Submit" />  
                            </form>

                    <?php }   
                    ?>

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

            <hr>

        </div>

答案 2 :(得分:2)

没有定义$ _POST ['submit']。 原因是该按钮没有name="submit"属性

答案 3 :(得分:0)

提交按钮中缺少名称

  <button type="submit" name ="submit" id="submit" class="btn btn-primary" data-loading-text="Loading...">Submit</button>  

答案 4 :(得分:0)

您将方法操作定义为 POST ,但您将其检索为 GET ,您也可以使用 $ _ REQUEST 从post / get获取变量请求。

更改此

if(isset($_GET['appid'])) {
                    // GET appid
                    $appid = $_GET['appid'];

if(isset($_REQUEST['appid'])) {
                    // GET appid
                    $appid = $_REQUEST['appid'];

也解决了案件。

  

$ _REQUEST中的变量通过GET,POST和COOKIE输入机制提供给脚本,因此可以被远程用户修改并且不可信任。此数组中列出的变量的存在和顺序是根据PHP variables_order配置指令定义的。

http://php.net/manual/pl/reserved.variables.request.php