Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to transfer php variables, that I have pulled out of a database, into three different javascript arrays. I use a php function to dynamically create a css dropdown menu, and then use a php loop to transfer the php variables into the javascript arrays. I then try to use a javascript function to initialize the attributes of the css menu. The same function is called from each of the links of the menu to update the menu based on the javascript arrays.

Here is the php function:

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>');
}

In the javascript part of the above function, I access a php class called vodObject- that just accesses the database and returns the strings I want to place in the javascript array.

The javascript function that updates the css menu is placed in the part of the html, and looks like this:

       <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>

The createfunc(i) is meant to get around the closure issues I heard about.

Any ideas about what I can try to fix my code? Thank you for your help, in advance!

share|improve this question
    
Bad bad code( the best way to get stuff from back-end - do asynchronous request –  Vladimir Gordienko Nov 27 '13 at 23:18

2 Answers 2

In the JS code, you can just use var city = '<?=$city;?>';, for example.

share|improve this answer
    
You can do that if you have js code in your html, but not if separate js file –  Vladimir Gordienko Nov 27 '13 at 23:22

If I got you right, you can make it using AJAX. The PHP file while print this:

echo json_encode($your_php_array);

Then, using JQuery $.getJSON method

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

Let me know if it's useful.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.