2014-10-09 156 views
0

我在路过一个js全局变量到PHP,所以我可以查询我的DB更多的数据的问题。我点击加载模式的链接来设置js变量。在这个例子中,变量是'id3'。我需要将它传递给php,以便将它包含在我的where语句中。谢谢!传递的Javascript全局变量到PHP

JavaScript的:

<script> 
$("#cluster_profiles_dataTables").on("click", "a", function() { 
    id3 = $(this).attr('id'); 
    document.querySelector('#clusterprofiletitlepulse').innerHTML = '<h4 class="modal-title" id="pulse">Profile Status' + ' (' + id3 + ')</h4>'; 
    document.querySelector('#clusterprofilepulsetable').innerHTML = '<i class="fa fa-long-arrow-right"></i> '+ id3 + ' Additional Anomalies'; 
    $('#cluster_profile_pulse_table').dataTable({ 
    "sAjaxSource": "../scripts/cluster_profile_pulse_table.php?val=" + id3, 
}); 

}); 
$('#clusterprofileanomalies').on('hidden.bs.modal', function() { 
    $('#cluster_profile_pulse_table').dataTable().fnDestroy(); 
}); 
</script> 

PHP:

<?php 
if (isset($_GET['id3'])) $id3 = $_GET['id3']; 
$sql = "SELECT PROFILE_ATTACHED FROM <databasehidden> WHERE CLUSTER_NAME = '".$id3."'"; 
$stmt = sqlsrv_query($conn, $sql); 
    if($stmt === false) { 
     die(print_r(sqlsrv_errors(), true)); 
    } 

    while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { 
    echo'<tr class="'.$class.'"> 
    <center>'.$row['PROFILE_ATTACHED'].'</center> 
    </tr>'; 
    } 

?> 
+1

你必须使用'$ _GET [ 'VAL']'。 ' – GuyT 2014-10-09 17:59:59

+0

这看起来[可怕不安全](http://bobby-tables.com/)。你确定**你的用户参数是[妥善转义](http://bobby-tables.com/php)? – tadman 2014-10-09 18:01:08

+0

''

不是一个有效的小孩'' ..in其实它是一个过时的标签 – charlietfl 2014-10-09 18:06:41

回答

1

由于你的Ajax源是:

"sAjaxSource": "../scripts/cluster_profile_pulse_table.php?val=" + id3, 

ID3为存储在VAL(VAL = ID3)。所以 您必须使用此代码来获取值:

if (isset($_GET['val'])) $id3 = $_GET['val']; 
+1

虽然这个片段可能会解决这个问题,这将是很好来形容你的方法,使未来的读者,具有不同但相似问题,从中受益。 – RandomSeed 2014-10-09 18:34:00

+0

@RandomSeed Thanx的建议。我添加了一些解释。这是否足够? – 2014-10-09 18:44:23

-2

你必须写你的变量在此模式下:

VAR ID3 = $(本).attr( '身份证');

马尔科

+0

当你这样做时,你不再有全局变量。如果未声明'var',则属性将被添加到'window'对象。 – GuyT 2014-10-09 19:25:53