使用 Ajax 从数据库访问数据


Access Data from Database with Ajax

我已经尝试了几乎所有的解决方案,我知道如果我在 php 中进行硬编码,我的数据库会返回值,所以问题是返回值或确定它是否实际上是作为 post 发送的。它进入成功功能并提醒已工作,但我无法定义项目 ID。

$(document).ready(function() {
$('.check').click(function(){
alert("Step1");
var thisID    = $(this).attr('id');
alert(thisID);
$.ajax({
    type: "GET",
    url: "XXX.PHP",
    data: { "ID": thisID},
    cache: false,
    async:true,
    datatype: "json",
    success: function(data)
    {
        alert("WORKED");
        alert(data.ItemID);
    }
});
}); 
});  

这是 PHP

if(isset($_POST['ID']))
{
$ID = $_POST['ID'];
function retrieve($ID)
{
$stmt = $mysqli->query("SELECT * FROM  database1.menu  WHERE ItemID = $ID");
if($stmt->num_rows) //if there is an ID of this name
{  
 $row = $stmt->fetch_assoc();
 echo $row;
 print json_encode($row);   
}
 }

你可以尝试这样的事情。不确定这是否是您需要的。

$(".check").click(function(event){
    event.preventDefault();
        var $this = $(this);
        var thisID = $this.attr('id');
        $.ajax({
             type: "POST",
             url: base_url + "url.php", 
             data: {
                 id: thisID          
             },
             dataType: "text",  
             cache:false,
             success: 
              function(data){
                 alert(data);
              }
         });
    return false;
});