Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
This.id使用ajax传递给php,但在jquery加载时返回未定义的索引_Php_Jquery_Ajax - Fatal编程技术网

This.id使用ajax传递给php,但在jquery加载时返回未定义的索引

This.id使用ajax传递给php,但在jquery加载时返回未定义的索引,php,jquery,ajax,Php,Jquery,Ajax,我一直在互联网上搜索,甚至在网站开发论坛上搜索,但幸运的是,我还没有找到解决问题的方法 这是html代码 <div id='bottom-right-display'> <ul id='display-app'> <li><a href='#' class='btn-test-1' id='123'>Testing1</a></li>

我一直在互联网上搜索,甚至在网站开发论坛上搜索,但幸运的是,我还没有找到解决问题的方法

这是html代码

<div id='bottom-right-display'>
                    <ul id='display-app'>
                        <li><a href='#' class='btn-test-1' id='123'>Testing1</a></li>
                        <li><a href='#' class='btn-test-2'>Testing2</a></li>
                        <li><a href='#' class='btn-test-3'>Testing3</a></li>
                    </ul>
</div>
$(".btn-test-1").click(function(){
        //with the use of post
        $.post("somefile.php",{id,this.id},function(data){
            $("#outputarea").load("somefile.php");
        });
        //or
        //with the use of ajax
        $.ajax({  
            type: "POST",  
            url: "somefile.php",  
            data: {id:this.id},      
            success: function(result){  
              $("#outputarea").load("somefile.php");
            } 
        })
    });
<?php
     require_once "connection.php";

     $sorter = $_POST["id"];//------>THIS IS THE LINE OF CODE WHERE THE ERROR IS STATING ABOUT!!
     $retrieve = $conn->prepare("SELECT * FROM `table1` WHERE `id` != '$sorter'");
     $retrieve->execute();
     $retrieve->bind_result($groupname);

     while($retrieve->fetch()){
        echo "<li>".$groupname."</li>";
     }
?>
这是php代码

<div id='bottom-right-display'>
                    <ul id='display-app'>
                        <li><a href='#' class='btn-test-1' id='123'>Testing1</a></li>
                        <li><a href='#' class='btn-test-2'>Testing2</a></li>
                        <li><a href='#' class='btn-test-3'>Testing3</a></li>
                    </ul>
</div>
$(".btn-test-1").click(function(){
        //with the use of post
        $.post("somefile.php",{id,this.id},function(data){
            $("#outputarea").load("somefile.php");
        });
        //or
        //with the use of ajax
        $.ajax({  
            type: "POST",  
            url: "somefile.php",  
            data: {id:this.id},      
            success: function(result){  
              $("#outputarea").load("somefile.php");
            } 
        })
    });
<?php
     require_once "connection.php";

     $sorter = $_POST["id"];//------>THIS IS THE LINE OF CODE WHERE THE ERROR IS STATING ABOUT!!
     $retrieve = $conn->prepare("SELECT * FROM `table1` WHERE `id` != '$sorter'");
     $retrieve->execute();
     $retrieve->bind_result($groupname);

     while($retrieve->fetch()){
        echo "<li>".$groupname."</li>";
     }
?>

检查数据是否确实在post请求中

i、 e

然后我还要检查您是否可以通过AJAX将数据发送到服务器。否则,请尝试将此代码添加到Javascript中

$(".btn-test-1").click(function(){

    var $dta = new FormData();

    $dta.append("id",this.id);

    $.ajax({  
        type: "POST",  
        url: "somefile.php",  
        data: $dta,
        contentType: false,
        cache: false,
        processData:false,
        success: function(result){  
          $("#outputarea").html(result);
        } 
    })
});

我认为问题在于,您首先发送一个附加了
id
的ajax请求,但在成功回调中,您再次请求
somefile.php
。第二次没有附加id

相反,您应该使用result变量从第一个请求获取somefile.php的“输出”。不需要第二个请求

像这样:


你的密码似乎没问题。但有一件事我无法模拟,那就是您的
必需的“connection.php”
。你能先简化你的php代码吗?我的意思是只需执行echo
$\u POST['id']
,以确保您前面的代码不会影响您的POST数据。

将jquery更改为此,然后尝试:

$(".btn-test-1").click(function(){
        var id_val = $(this).attr('id');
        //with the use of ajax
        $.ajax({  
            type: "POST",  
            url: "somefile.php",  
            data: {id:id_val },      
            success: function(result){  
              $("#outputarea").load("somefile.php");
            } 
        })
    });

检查数据是否确实在post请求中。IE:
if(isset($\u POST['id'))$sorter=$\u POST['id');否则死亡('ERROR:No ID set')
{ID,this.ID}
替换为
{ID:this.ID}
?我还注意到您在第一个
  • 上只有一个
    ID=”“
    ,其他两个
  • 没有
    ID=“”
    传递给PHPscript@JacquesKoekemoer我试过了,但遗憾的是显示的是错误。@TobiasXy我已经这么做了,但仍然是未定义的。希望你不介意
    IE:
    看起来像是你在说话和上网Explorer@RiggsFolly谢谢:)事实上,我没有意识到我已经完成了isset,但遗憾的是它是普遍存在的错误。我尝试了你的代码,但仍然有相同的结果。我不知道这是否真的是问题所在,因为当我删除$(“#outputrea”).load(“somefile.php”);但将其更改为警报(结果)我想要显示的结果,但当我使用加载时,未定义的索引错误就是其中之一displaying@Middle-级别-很抱歉,我实际上犯了一个错误,它不应该是
    .load
    它应该是
    .html
    。load命令实际上执行了一个简化的
    GET
    页面请求。这个命令确实有效!。对于所有在这方面帮助我的人,我真的感谢你们所有人,因为我3天的研究现在已经完成,这是因为你们所有人。感谢您的回答。仍然是相同的错误使用html(结果)未定义索引,但问题是当我这样做时,在jquery中执行警报(结果)时,它会显示它传递了数据,但当我使用.load或.html时,它仍然有未定义索引错误。这很奇怪。然后按照Jacques在对您的问题的评论中的建议,向我们展示
    $\u POST
    变量的内容。required connection.php根本没有任何问题,唯一的问题是使用ajax或POST传递单击元素的id值,但实际上它变为空