在mysql查询中使用javascript变量并填充listview

在mysql查询中使用javascript变量并填充listview,javascript,php,jquery,jquery-mobile,Javascript,Php,Jquery,Jquery Mobile,我有一个GoogleMap/marker应用程序,我想在mysql查询中使用marker id,以便在单击标记时在listview中提取与标记相关的数据 该变量是在单击标记时分配的,我正在使用ajax将值传递到php文件,以便在mysql查询中使用该变量 JS 成功后,我调用了getoffers函数,该函数用数据库值填充listview var output1 = ''; function getoffers() { $.post( "getoffer.php?getjso

我有一个GoogleMap/marker应用程序,我想在mysql查询中使用marker id,以便在单击标记时在listview中提取与标记相关的数据

该变量是在单击标记时分配的,我正在使用ajax将值传递到php文件,以便在mysql查询中使用该变量

JS

成功后,我调用了getoffers函数,该函数用数据库值填充listview

var output1 = '';       
function getoffers() {
    $.post( "getoffer.php?getjson", function( data ) {
        var data = JSON.parse(data);
        console.log(data);
        $.each(data, function(index, value){
            output1 += '<li data-id='+ value.offer_id +'><a href="#"">' + value.offer_title + '</a></li>';  
        });
        $('#offerlist').html(output1).listview().listview('refresh');
    });
}
PHP

运行应用程序时没有错误,但listview为空

当我从sql中删除ajax代码和where子句时,代码将返回该表中的所有值,因此我知道它与db连接和listview的输出有关。我一定是在ajax中传递的变量出错了,或者我调用的代码不正确


非常感谢您的建议。谢谢你

出于某种原因,你在success函数中发出了一个单独的ajax请求。success函数已经将数据传递给它,因此只需删除第二个ajax请求:

function getoffers(data) {
    //$.post( "getoffer.php?getjson", function( data ) {
        var data = JSON.parse(data);
        console.log(data);
        $.each(data, function(index, value){
            output1 += '<li data-id='+ value.offer_id +'><a href="#"">' + value.offer_title + '</a></li>';  
        });
        $('#offerlist').html(output1).listview().listview('refresh');
    //});
}
或者更改您的php以使用关键站点:


在第一个示例中,您正在发布一个名为site的值,但在PHP中,您正在检索site_id。不确定为什么要在getoffers中再次发布到getoffer.PHP,并且没有任何site或site_id。您可能只想将数据作为getoffers.and Paul Roub的参数。谢谢各位帮我解决了这个问题:我没意识到我给阿贾克斯打了两次电话。非常感谢。
<?php
$site = $_POST['site_id'];
require("dbconnect.php");
try {
    $DBH = new PDO("mysql:host=$server;dbname=$database;charset=utf8", $username, $password,
      array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
}
catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

$STH = $DBH->prepare("SELECT * FROM Offer WHERE site_id='$site'");
$STH->execute();
$arr = $STH->fetchAll();
echo json_encode($arr);
die();
function getoffers(data) {
    //$.post( "getoffer.php?getjson", function( data ) {
        var data = JSON.parse(data);
        console.log(data);
        $.each(data, function(index, value){
            output1 += '<li data-id='+ value.offer_id +'><a href="#"">' + value.offer_title + '</a></li>';  
        });
        $('#offerlist').html(output1).listview().listview('refresh');
    //});
}
data: { site_id: site_id},
$site = $_POST['site'];