【问题标题】:isset causing getjson to return undefinedisset 导致 getjson 返回 undefined
【发布时间】:2013-02-22 22:58:45
【问题描述】:

我的代码有问题。我想通过在表单有效时创建一个包含变量的数组来验证我的表单。但要做到这一点,我需要使用 isset 方法才能知道信息已经发布。这是一个简单的例子

http://richbaird.net/clregister

<?PHP

if(isset($_POST['username'])) {

$helloworld = array ("hello"=>"world","name"=>"bob");


print json_encode($helloworld);

};

if(!isset($_POST['username'])) {

echo json_encode(array('error' => true, 'message' => 'No username specified'));



?>


如果用户名已经发布就很简单了,创建数组helloworld。

我正在使用以下方法获取json

<script>

//document ready

$(document).ready(function(){

var php = "helloworld.php";

//submit form
$("#loginform").ajaxForm
(

//on successful submission

function() {

//getjson

$.getJSON("helloworld.php",function(data) {

    alert(data.message)

}) //close get json


.error(function(error) { alert(error.responsetext); })
.complete(function() { alert("complete"); });

} // close success

) // close submit




});
//end document ready
</script>

我正在使用 jquery forms 插件提交表单。

我的表单看起来像这样

<form id="loginform" name="loginform" method="post" action="helloworld.php">
<label for="username">username</label>
<input type="text" name="username" id="username" />

<br />
<label for="password">password</label>
<input name="password" type="password" />

<br />
<input name="submit"  type="submit" value="Login" id="subtn" />

</form>

网络控制台显示方法 POST 返回 {hello:world name:bob} 但 GET 返回未指定用户名,这是我在警报中得到的。看起来 jquery 正在尝试在有机会完全处理之前获取代码,我该如何防止这种情况发生?

【问题讨论】:

    标签: php jquery json forms validation


    【解决方案1】:

    经过几个小时的思考和 juco 的大量帮助,我意识到,我在这个函数中进行了 2 个单独的调用。首先我发布了有效的数据,然后在一个成功的帖子上我试图进行单独的调用,一个 GET 请求,该请求包含应该提醒我结果的回调,但是因为它是第二个调用并发送一个 GET 请求,变量 POST 从未设置,因此没有什么可取回的。我修改了我的代码,只使用 post 方法。

    <script>
    
    //document ready
    
    $(document).ready(function(){
    
    
    
    
    
    // bind form using ajaxForm 
    $('#loginform').ajaxForm({ 
        // dataType identifies the expected content type of the server response 
        dataType:  'json', 
    
        // success identifies the function to invoke when the server response 
        // has been received 
        success:   processJson 
    }
    
    
    
    
    
    ); 
    
    
    function processJson(data) {
    
    alert(data.hello);
    
    }
    
    
    
    
    });
    //end document ready
    

    【讨论】:

      【解决方案2】:

      您缺少引号。应该是:

      if(isset($_POST['username']))
      

      您应该检查您的控制台,看看username 是否真的被发布了,好像不是,您没有返回任何数据。您可以考虑返回错误if(!isset($_POST['username'])),可能类似于:

      echo json_encode(array('error' => true, 'message' => 'No username specified'));
      

      编辑 另外,请记住这是$_POST,而不是$_post

      二次编辑

      您的代码将更加直观和可读,如下所示:

      $return = array();
      if(isset($_POST['username'])) {
          $return = array("hello"=>"world","name"=>"bob");
      } else {
          $return = array('error' => true, 'message' => 'No username specified');
      }
      echo json_encode($return);
      

      【讨论】:

      • 很好,我添加了引号但仍然得到相同的错误。查看编辑
      • 我现在做了一些修改!但最后一点可能是最重要的 ;-)
      • 酷,所以我进行了更新并按照你说的做了,我收到错误没有指定用户名,这显然意味着它没有与帖子一起发送。我现在的问题是为什么不。我的控制台的网络选项卡清楚地写着 helloworld.php 方法帖子。您是否知道某种方式可以准确查看发送的内容?预览还给了我 { hello: "world" name: "bob" }
      • 您是否也将$_post 更改为$_POST? (区分大小写)
      • 通过重构代码进行的另一个编辑。您还应该尝试var_dump($_POST); exit; 并在控制台中检查结果。
      猜你喜欢
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 2019-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-18
      相关资源
      最近更新 更多