-2
我有一個簡單的按鈕,調用一個js函數。在那裏我想用動態名稱的PHP變量。有什麼想法嗎?js獲取php動態名稱變量
<?php
$counter = 1;
${"fotos" . $counter++} = array();
foreach ($row as $item) {
${"fotos" . $counter}[] = $item->img;
}
?>;
//will generate two vars:
$fotos = [img1];
$fotos1 = [img1,img2];
<button type="button" onClick="verAdjuntos(<?php echo $counter; ?>)" class="btn btn-xs btn-default"> View</button>
//parameter passed verAdjuntos(1) forxample
當id參數爲空時,if條件正常工作。但是,增加ID到PHP變量名稱是問題...
<script type="text/javascript">
function verAdjuntos(id){
if (id == null){
var fotos = <?php echo $fotos ?>; //output ok
alert(JSON.stringify(fotos));
}else{
var fotos = <?php echo $fotos?> + id; /// how do name $fotos1 here?
alert(JSON.stringify(fotos));
};
}
</script>
做好警戒工作也沒關係:
alert(JSON.stringify(fotos1));
問題?問題是什麼? – nicael
我覺得你很困惑。 PHP運行在服務器上,客戶端上的JS。他們不能直接互動。但從這個問題我不能確定,如果這就是問題所在...... –
nope,從服務器我生成多個php變量,並從客戶端Ÿ只能檢索這些變量的名字 –