在php函数中将php变量转换为javascript变量


Transfering php variables to javascript variables in a php function

我正试图将从数据库中提取的php变量转移到三个不同的javascript数组中。我使用php函数动态创建css下拉菜单,然后使用php循环将php变量转移到javascript数组中。然后,我尝试使用javascript函数来初始化css菜单的属性。从菜单的每个链接调用相同的函数,以基于javascript数组更新菜单。

以下是php函数:

function createMenu()
{
    //create the proper drop down menu, based on the total number of games played and the max number of games
     $totalVods = count($this->VodID);
     $menuArray = array();
     for($i = 0; $i < $totalVods; ++$i)
     {
         if ($this->currentVodID != $i)
         {
            $menuArray[] = ($i+1); 
         }
     }
     if ( $totalVods < $this->MaxGames )
     {
        for ($i = $totalVods; $i < $this->MaxGames; ++$i)
        {
            $menuArray[] = ($this->currentVodID+1);
        }
     }
     $totalGames = count($menuArray);
            printf("<p>$this->Name</p>");

    printf("<div id='cssmenu2'>");
    printf('<ul>');
    if ($menuArray)
    {
        printf('<li id="vod1" class="has-sub last"><span></span><img class="arrow" src="/images/Arrow.png">');
        printf('<ul>'); 
        if ($totalGames > 1)
        {
            for ($j = 0; $j < ($totalGames - 1); ++$j)
            {
                $vodNum = 'vod'. ($j+2);
                printf("<li id='$vodNum' onclick=''><span></span></li>");
            }
        }
        $vodNum = 'vod'. ($totalGames + 1);
        printf("<li id='$vodNum' class='last' onclick=''><span></span></li>");
        printf('</ul>');
    }
    else
    {
        printf('<li id="vod1"><span>Game 1</span>');
    }
    printf('</li>');
    printf('</ul>');
    printf('</div>'); 
            //---------------------------------------------------
    // function works fine up till here
            // ------------------------------------------------
    printf('<script>'); 
    printf('alert("Right after totalGames");'); /* If I take this part out, up till the next comment, the rest works, but none of this part, even the alerts, works */
    for($i = 0; $i < $totalVods; ++$i)
    {
        printf("vodName[$i] = 'Game ". ($i + 1) ."';");
        printf("alert('vodName[$i]= '+vodName[$i]);");
        $this->VodObject->selectVod($this->VodID[$i]);
        $address = $this->VodObject->returnVod();
        printf("vodAddress[$i] = '$address';");
        $date = $this->VodObject->returnDate();
        printf("vodDate[$i] = '$date';");
    }
    if ($totalVods < $this->MaxGames )
    {
        for ($i = $totalVods; $i < $this->MaxGames; ++$i)
        {
            printf("vodName[$i] = 'Game ". ($i + 1) ."';");
            $gameNum = $i +1;
            $address = "<div class='matchNoVOD'>There is no Game $gameNum<br />Series ended before this game.</div>";
            printf("vodAddress[$i] = '$address';");
            printf("vodDate[$i] = '';");
        }
    }/* End of the section that is having problems- if I take this out, the rest of the alerts work */
    printf('alert("Right before window.onload");');
    printf('window.onload = function() { switchVod(0); };');
    printf('</script>');
}

在上面函数的javascript部分,我访问了一个名为vodObject的php类,该类只访问数据库并返回我想放在javascript数组中的字符串。

更新css菜单的javascript函数位于html的部分,看起来如下:

       <script>
       var vodName = Array();
       var vodAddress = Array();
       var vodDate = Array();
        function createfunc(i)
        {
            return i;
        }
        function switchVod(vodID)
       {        
                alert("switchVod ran");
                alert('vodID= ' + vodID);
                var x=document.getElementById("vod1");
                var y = x.getElementsByTagName("span");
                y[0].innerHTML = vodName[vodID];
                for (var i=0; i < vodName.length; i++)
                {
                        if ( i != vodID)
                        {
                            var id = createfunc(i);
                            var gameNum = i + 2;
                            var gameID = "vod" + gameNum;
                            var x = document.getElementByID(gameID);
                            var y = x.getElementsByTagName("span");
                            y[0].innerHTML = vodName[i]
                            x.onclick = function() { switchVod(id); }
                        }
                }
                alert("after for loop");
                alert("1");
                document.getElementById('vodObj').innerHTML = vodAddress[vodID];
                alert("2");
                document.getElementById("vodDate").innerHTML = vodDate[vodID];
                alert("finished");
        }
       </script>

createfunc(i)旨在解决我听说的闭包问题。

关于如何修复我的代码,有什么想法吗?提前感谢您的帮助!

例如,在JS代码中,您可以只使用var city = '<?=$city;?>';

如果我说得对,你可以使用AJAX。打印此文件时的PHP文件:

echo json_encode($your_php_array);

然后,使用JQuery$.getJSON方法

$.getJSON('json.php', function(response) {
    /* do your stuff here */
    console.log(response); // This var has the json object
});

如果有用,请告诉我。