【问题标题】:PHP_SELF on submit form [closed]提交表单上的 PHP_SELF [关闭]
【发布时间】:2013-05-23 09:17:54
【问题描述】:

我有一个关于在表单提交中使用 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。
是 -> 从 api 获取数据
否 -> 显示表单以便您可以插入 appid

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

我做错了什么?

【问题讨论】:

  • 删除form's action 的值,因为您正在同一页面中处理表单。并将methodpost 替换为get
  • 您的表单方法设置为 POST。您正在使用 GET 来检查 appid
  • 如果你使用方法 post 你应该尝试使用 $_POST['appid'] 而不是 $_GET['appid']
  • 对不起,愚蠢的错误!谢谢大家!

标签: php forms get isset


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    试试这个....

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        提交按钮中缺少名称

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

        【讨论】:

          【解决方案5】:

          您将方法操作定义为 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

          【讨论】:

            猜你喜欢
            • 2016-12-09
            • 1970-01-01
            • 2012-03-10
            • 2015-10-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多