我想通過php會話[第一頁]通過一個變量通過ajax fancybox [第二頁]調用一個變量,但我只獲得我的數組的最後一個值而不是選擇時需要的當前值。發送變量通過php會話形式通過fancybox 3 ajax
下面的代碼:
[首頁]
<?php
session_start();
$africa = array('algeria.png','angola.png', ...);
$africa_length = count($africa);
for($i = 0; $i < $africa_length; $i++){
$replace = str_replace(array(".png", "-"), " ", $africa[$i]);
$return = '<a data-fancybox="ajax" data-src="test.php" data-type="ajax" href="javascript:;">
<div class="international-item">
<div class="flag" style="background:url(img/flags/africa/'.$africa[$i].') no-repeat"></div>
<div class="txt_bold_2 nation">'.$replace.'</div>
</div>
</a>';
echo $return;
$_SESSION['varname'] = $africa[$i];
}
?>
[第二頁]
<?php session_start();?>
<form>
...
</form>
<?php
$var_value = $_SESSION['varname'];
echo ($var_value);
?>
我必須[首頁]選擇國家要傳遞給表單[SECOND PAGE],因此我可以在提交時將其插入到數據庫中。
Thx提前尋求幫助。
這是正確的。在URL中傳遞值是我最好的解決方案。通過$ _SESSION傳遞它太複雜了,因爲@Arun Kumaresh說「因爲循環運行會話變量每次都會得到更新」,所以我無法捕獲當前變量,只能捕獲數組中的最後一個變量。 – pitinca