Button not working in div after ajax refresh of its content

I have a div with some buttons in it. The buttons should only appear if a certain event has occurred. My web app allows users to log into rooms and vote on something. If user (let’s say, joe) logs into room 20A and user dan also logs in. If dan presses the create poll button on his page, a vote button should then appear on joes page. I’m using ajax as I haven’t been able to find a good example of jquery.

Here’s the ajax function


<script type="text/javascript">

   function ajaxUpdateVotingButtons(){

	var votingStatusRequest;  // The variable that makes Ajax possible!

	

	try{

		// Opera 8.0+, Firefox, Safari

		votingStatusRequest = new XMLHttpRequest();                

	} catch (e){

		// Internet Explorer Browsers

		try{

			votingStatusRequest = new ActiveXObject("Msxml2.XMLHTTP");                        

		} catch (e) {

			try{

				votingStatusRequest = new ActiveXObject("Microsoft.XMLHTTP");                               

			} catch (e){

				// Something went wrong

				alert("Your browser doesn't seem to support ajax!");

				return false;

			}

		}

	}

	// Create a function that will receive data sent from the server

	votingStatusRequest.onreadystatechange = function(){

                var voteMessage = document.getElementById('voting_buttons');                

		if(votingStatusRequest.readyState == 4)

                {

                    voteMessage.innerHTML = votingStatusRequest.responseText;                    

                }                

    }

	votingStatusRequest.open("GET", "index.php?r=room/ShowVoteButtons", true);

	votingStatusRequest.send(null); 

        

}

</script>



The ajax function loads this div below with content from another file


<div id="voting_buttons">

		

     <?php 

       $user = User::model()->find('username=?', array(Yii::app()->user->name));                                                                                        

       $poll = Poll::model()->find('room_name=? AND in_session=?',array($user->room_name, 1));        

                

        if(isset($poll) && $poll->in_session == 1) // if a poll has been started

        {   

            if($user->has_voted ==1)

            {            

              echo CHtml::submitButton('Vote',array('disabled'=>'disabled')); 

            }

            else

            {

              echo CHtml::submitButton('Vote', array('submit' => array('vote/Create')));

            }

        }

        ?>

</div>

the file used to to fill the above div


<?php 

        $user = User::model()->find('username=?', array(Yii::app()->user->name));                                                   

        $poll = Poll::model()->find('room_name=? AND in_session=?',array($user->room_name, 1));

        

        if(isset($poll) && $poll->in_session == 1) // if a poll has been started

        {   

            if($user->has_voted ==1)

            {            

              echo CHtml::submitButton('Vote',array('disabled'=>'disabled')); 

            }

            else

            {

              echo CHtml::submitButton('Vote', array('submit' => array('vote/Create')));

            }

        }

                

?>

Yes it’s exactly the same logic, I realize this may be quite backward but given my level of experience with both php and javascript it’s all I’ve managed to find thus far. The ajax function above is called every 5 seconds via a setInterval I have (not shown here)

So the problem is that when a poll is started, using this condition to check for that


$poll = Poll::model()->find('room_name=? AND in_session=?',array($user->room_name, 1));

The div’s content is reloaded just fine, but the button will not work. I have to refresh the page manually in the browser in order for the button to work (call the vote/Create) action. Do I need to bind an event to the vote button via javascript after the div reload? Any idea what I’m missing?

Determine if the user has voted in the controller (index.php?r=room/ShowVoteButtons), not the view.

If he hasn’t, just set the innerHTML to render the button. No need for the rest of the code in your response.

Ok so if I’ve moved that piece of logic (determining if the user has voted) to the controller and it renders the button, sound advice by the way. The button still won’t work, it won’t actually run the action unless I refresh the page. The logic is sound as it only renders the button when it’s supposed to. Any ideas what i could be missing?

Ok so I’m still having no luck on this, below is the code I’m currently using. Just to recap, here is my problem. Users log into a room, one user in the room is the moderator, the rest are just users. A moderator can start a poll, when he/she does this, everyone’s page should update by having a button appear on their page.


<script type="text/javascript">

   

   function ajaxUpdateVotingButtons(){

	var votingStatusRequest;  // The variable that makes Ajax possible!

	

	try{

		// Opera 8.0+, Firefox, Safari

		votingStatusRequest = new XMLHttpRequest();                

	} catch (e){

		// Internet Explorer Browsers

		try{

			votingStatusRequest = new ActiveXObject("Msxml2.XMLHTTP");                        

		} catch (e) {

			try{

				votingStatusRequest = new ActiveXObject("Microsoft.XMLHTTP");                               

			} catch (e){

				// Something went wrong

				alert("Your browser doesn't seem to support ajax!");

				return false;

			}

		}

	}

	// Create a function that will receive data sent from the server

	votingStatusRequest.onreadystatechange = function(){

                

                var voteButtonDiv = document.getElementById('voting_buttons');

                

		if(votingStatusRequest.readyState == 4)                

                {                                             

                    voteButtonDiv.innerHTML =  votingStatusRequest.responseText;                      

                }                

    }

	votingStatusRequest.open("GET", "index.php?r=room/ShowVoteButton", true);

	votingStatusRequest.send(null); 

                        

       

}

</script>

This is the ajax function which runs every 5 seconds. It is constantly checking to see if a poll has been started. The votingStatusRequest.open("GET", "index.php?r=room/ShowVoteButton", true); calls the below action to check if a poll has been started.


<?php     

   public function actionShowVoteButton()

    {   

        

        $user = User::model()->find('username=?', array(Yii::app()->user->name)); 

        

        if(isset($user))

        {

            $poll = Poll::model()->find('room_name=? AND in_session=?',array($user->room_name, 1));

        }

       

        if($user->has_voted == 0 && isset($poll))

        {

            $this->renderPartial('renderVoteButton');        

        }

        

        if($user->has_voted == 1 && isset($poll))

        {

             $this->renderPartial('disableVoteButton');        

        }

    }

?>



So if a poll has been started, and the user has not already voted, the below script is run by the first renderPartial statement


<?php     

    echo CHtml::submitButton('Vote', array('submit' => array('vote/Create'),'name'=>'vote'));

?>



So the echoed content from this script is rendered inside the voting buttons div, the code for the voting buttons div is below




<div id="voting_buttons">

    	

     <?php                              

        if(isset($poll) && $poll->in_session == 1) // if a poll has been started

        {   

                      

            if($user->has_voted ==1)

            {            

              echo CHtml::submitButton('Vote',array('disabled'=>'disabled')); 

              

            }

            else

            {

              echo CHtml::submitButton('Vote', array('submit' => array('vote/Create'),'name'=>'vote'));                

            }

        }

      ?>

</div>



So when a poll has been started, the vote button is rendered inside this div. Bear with me here, so the button rendered/echoed by the ajax call won’t work, but when I refresh the page, i.e. reload it, it is the logic inside the voting buttons div that runs and echoes the vote button, then the button does work. Hope that makes sense, can anyone help me, I’ve been trying to solve this for days and am getting nowhere.