0

I would like to use a popup in for loop to permit to the user different number.

The number of loop will be random.

I try this code but I never get the first value only the second value (in this example, only 2 loop)

<?php
session_start();
?>
<script language="JavaScript">

function win1() {
    window.open("try.php","Window1","menubar=no,width=460,height=360,toolbar=no");
}
</script>
<?php
for($j = 1; $j <= 2; $j++){

$_SESSION["j"] = $j;
?>
<p><a href="javascript:win1()" onMouseOver="self.status='Open A Window'; return true;"><b>Open Window Example 1</b></a></p>
<?php
echo $j;
}
?>

How can I do to have in the 1st link the 1st value and in the second link the second value?

4

1 回答 1

0

在您的代码中,为每次迭代设置会话 j。第一个循环 = $_SESSION["j"] 设置为 1,第二个循环 $_SESSION["j"] 设置为 2(覆盖第一个循环)。

<?php
for($j = 1; $j <= 2; $j++){
    $_SESSION["j"] = $j;
    ?>
    <p><a href="javascript:win1()" onMouseOver="self.status='Open A Window'; return true;">     <b>Open Window Example 1</b></a></p>
    <?php
    echo $j;
    }
?>

您不能以这种方式在服务器端语言 php 和 javascript 之间发送会话。我认为你想要实现的是这样的:(将迭代的 nr 发送到 js 函数)

<?php
for($j = 1; $j <= 2; $j++){
    ?>
    <p><a href="javascript:win1('<?php echo $j;?>')" onMouseOver="self.status='Open A Window'; return true;">     <b>Open Window Example 1</b></a></p>
    <?php
    }
?>

并在您的 js 中确保将迭代的值作为参数添加到 url:

<script type="text/javascript">    
    function win1(j) {
        window.open("try.php?j=" + j ,"Window1","menubar=no,width=460,height=360,toolbar=no");
    }
</script>

然后在 try.php中根据变量 j 设置会话。

<?php
session_start();
$_SESSION["j"] = $_GET['j'];
...
...
?>
于 2013-07-25T01:35:47.943 回答