Ajax POST或PHP中的未定义索引

我正在尝试向以下div添加内容:

<div id="radio-toolbar" class="radio-toolbar">

</div> 

使用此AJAX脚本:

$('#part_code').keyup(function(){
    $.ajax({ 
        url: "binlist.php",
        data: {text: $(this).val()},
        type: "POST",
        dataType:'json',        
        success: function(data)
                {
                    $("#radio-toolbar").html(data);
                }
        });
    });

我从中获取数据的输入就是这个。编写文本时,ajax脚本将触发:

<input type="text" name="part_code" id="part_code" class="form-control" value="<?php echo $part;?>">

Binlist.php代码在SQL Server中进行一些检查,然后编写HTML代码。 如果我硬设置$ part_code_for_bin变量,它可以工作,但是当我尝试从POST获取它时,出现错误“未定义的索引:part_code”

<?php

include 'code\database.php';
include 'code\plant.php';

//$part_code_for_bin = '1020';
//$part_code_for_bin = '116-SIG-30622A-TSM260A';

$part_code_for_bin = $_POST['part_code'];

if(isset($part_code_for_bin)) { 

    $sql_binlist= "SELECT BIS_BIN FROM R5BINSTOCK WHERE BIS_PART = '$part_code_for_bin' and BIS_STORE = '$store' and BIS_PART_ORG = '$org'";

    $bins = sqlsrv_query($conn, $sql_binlist, array(), array( "Scrollable" => 'static'));
    $bins_count = sqlsrv_num_rows($bins);

    if ($bins_count > 1) { 

        $result_bins = array();
        $i = 0;

        while ($row = sqlsrv_fetch_array ($bins, SQLSRV_FETCH_ASSOC)) {
            $result_bins[] = $row['BIS_BIN'];
            ?>
                <input type="radio" id="<?php echo $result_bins[$i]?>" name="radioBin" value="<?php echo $result_bins[$i]?>" onclick="write_bin('<?php echo $result_bins[$i]?>');">
                <label for="<?php echo $result_bins[$i]?>"><?php echo $result_bins[$i]?></label>

            <?php
            $i++;
        }

    }
}

?>
评论
  • dab
    dab 回复

    您有两个错误:

    1. You define text in your Ajax request but try to access part_code in PHP.

      data: {part_code: $(this).val()},

    2. You first try to save the POST value to a variable and then check if it is existing. You have to to do the isset() first before saving it to a variable.

      if(isset($_POST['part_code'])) { $part_code_for_bin = $_POST['part_code'];