Codeigniter ajax post值未定义错误在索引页


codeigniter ajax post value undefined error on index page

我正在编写编码器web应用程序,我需要从数据库中获取用户的数据,但基于用户id?

问题是ajax向浏览器的索引页张贴值,网络显示Ajax向索引页张贴值,当我尝试在索引页上回显或打印变量时。

索引页显示Echo leadID后出现错误

Notice: Undefined index: leadID and leadID = 0

索引页

$addnote ="<a class='btn' data-popup-open='popup-1' href='#' 
data-id='$lead->id'>Add Note</a>";   
Ajax发布

    $('.btn').on('click', function(e)  {
    var targeted_popup_class = jQuery(this).attr('data-popup-open');
    $('[data-popup="' + targeted_popup_class + '"]').fadeIn(350);
        var leadID = $(this).attr('data-id');
        var dataString = 'leadID=' + leadID; 
        $.ajax({
            type: "POST",  
            dataType: 'text',
            url: "index.php",  
            data: dataString,  
            success: function(response) {

            }
        }); 
    });
数据库查询

$leadID = intval($_POST['leadID']);
$sql = "select Note from {$dbPre}users where leadID='$leadID'";  
$exists = $db->extQueryRowObj($sql);
$displaynote = $exists->Note;

如果您需要简单的php解决方案,请尝试以下代码

$('.btn').on('click', function(e)  {
    var targeted_popup_class = jQuery(this).attr('data-popup-open');
    $('[data-popup="' + targeted_popup_class + '"]').fadeIn(350);
        var leadID = $(this).attr('data-id');
        $.ajax({
            type: "POST",  
            dataType: 'text',
            url: "index.php",  
            data: {
                leadID : leadID
            }
            success: function(response) {
               alert(response);
            }
        }); 
    });

现在在index.php

$leadID = intval($_POST['leadID']);
$sql = 'select Note from '.$dbPre.'users where leadID='.$leadID;  
$exists = $db->extQueryRowObj($sql);
$displaynote = $exists->Note;
echo $displaynote;