Javascriptvariable in PHP

Javascriptvariable in PHP

本文关键字:PHP in Javascriptvariable      更新时间:2023-09-26
我有一个问题。我在Javascript中有这个for循环,在PHP中有一些Sessionvariables,声明如下:PHP:
$Vorname = $_POST['Vorname'];
$_SESSION['anzahl'.$Vorname] = $_POST['anzahl'];

会话变量不止一个,如SESSION['anzahl'.'div0']SESSION['anzahl'.'div1']SESSION['anzahl'.'div2']

在Javascript中,我想从不同的Sessionvariables中获得一个循环的值。

如何从js -for循环中获得"i"到PHP回显?还是有更好的方法?JS:

for(var i =0;i<Bestellungen;i++){
     Sessionanzahl.push("<?php echo $_SESSION['anzahl'."div".i]?>");
}

谢谢!

将整个php数组作为json传递给javascript变量并合并到其他数组

var phpArray = <?php echo json_encode($_SESSION['anzahl']) ?>;
Sessionanzahl = Sessionanzahl.concat(phpArray);