繁体   English   中英

将Javascript全局变量传递到PHP

[英]Passing Javascript Global Variable into PHP

我在将js全局变量传递给PHP时遇到问题,因此我可以查询数据库以获得更多数据。 我单击加载模态的链接时设置js变量。 在此示例中,变量为“ id3”。 我需要将其传递到php中,以便可以将其包含在我的where语句中。 谢谢!

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>';
    }

?>

由于您的ajax来源是:

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

id3存储在val(val = id3)中。 因此,您必须使用以下代码来获取值:

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

您必须在这种模式下编写变量:

var id3 = $(this).attr('id');

马可

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM