codeigniter ajax post value索引页面上的未定义错误

时间:2016-11-01 10:17:24

标签: php ajax database codeigniter

我正在开发codeigniter 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;

1 个答案:

答案 0 :(得分:1)

如果您需要简单的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;