2014-10-09 158 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