Search code examples
phpjqueryajaxfancybox

Send variable via php session to form via fancybox 3 ajax


I'm trying to pass a variable via php session [FIRST PAGE] to a form called via ajax fancybox [SECOND PAGE] but I only obtain the last value of my array and not the current value I need when selected.

Here the code:

[FIRST PAGE]

<?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];
}
?>

[SECOND PAGE]

<?php session_start();?>
<form>
...
</form>

<?php
$var_value = $_SESSION['varname'];
echo ($var_value);
?>

I need to have the selected nation in [FIRST PAGE] to be passed to the form [SECOND PAGE] so I can insert it in a DB when submitted.

Thx in advance for any help.


Solution

  • If I understand correctly, you just have to pass this value for the next page using $_GET. Simply create links like this:

    data-src="test.php?varname='.$africa[$i].'"