2013-11-27 49 views
0

我試圖將我已經從數據庫中提取出來的php變量轉換爲三個不同的javascript數組。我使用php函數動態創建一個css下拉菜單,然後使用php循環將php變量轉換爲javascript數組。然後我嘗試使用JavaScript函數來初始化css菜單的屬性。從菜單的每個鏈接調用相同的函數,以基於javascript數組更新菜單。在php函數中將php變量傳遞給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,放入陣列。

JavaScript函數,更新的CSS菜單被放置在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(我)是爲了避開我聽說過封的問題。

有關我可以嘗試修復我的代碼的任何想法?提前謝謝你的幫助!

+0

糟糕的錯誤代碼(從後端獲取內容的最佳方式 - 執行異步請求 –

回答

0

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

+0

如果您的html中包含js代碼,但如果單獨的js文件沒有,則可以這樣做 –

0

如果我找到了你,你可以使用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 
}); 

讓我知道,如果它是有用的。