Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

How to use a global variable outside the script tag

Avatar of Mark Brady
Mark BradyFlag for United States of America asked on
PHPJScriptAJAX
1 Comment1 Solution400 ViewsLast Modified:
I have a code which uses javascript and ajax to get values from a php script. That works well. My problem is I need to read and display the value of a counter which is decared and incremented inside a <script> tag outside of the script. In other words, once I get to </script> I start using <div> tags to display data. I would like to be able to display the current counter number that is produced inside the javascript functions. The variable I need to use is "questionNo". This counter is incremented each time ajax sends a request form the php script. Now I need to be able to use this value on the same static page but outside of the script. I have attached the php script with the variables.

This must be a super easy thing to do for anyone understanding javascript poperly. I have tried decalring it as a global variable but can't seem to access it. Anyway, take a look at the code and please let me know how to get and display the counter so I can use it within a <div> tag for easy display.
<?php
include("game.php");
$reset = $_GET['reset'];
if($reset == 1) include("reset.php");

?>
<html>
	<head>
<script type="text/javascript" src="miniajax.js"></script>
<link type="text/css" rel="stylesheet" href="style.css" />
<script language="javascript">
                var globalTimer,questionTimer;
                var questionNo=0,interval=<?php echo $_session['delay1']; ?>,counter=interval;
                var data;
                var digits = [
				    "digits/0.gif",
				    "digits/1.gif",
				    "digits/2.gif",
				    "digits/3.gif",
				    "digits/4.gif",
				    "digits/5.gif",
				    "digits/6.gif",
				    "digits/7.gif",
				    "digits/8.gif",
				    "digits/9.gif" ];
				var spacer = ["digits/spacer.gif"];
                function init()
                {
                        globalTimer=setInterval("checkCounter()",100);
                        data=document.getElementById("data");
                        getNewQuestion();
                }
                function checkCounter()
                {
                        if (counter==0)
                        {  
                            if (questionNo<10) 
                            {	
	                            getNewQuestion();
                                  

                        	}   
                        	else
                        	{
	                        	document.getElementById('question').innerHTML='All Done !';
                                        clearInterval(questionTimer); // Stop the timer
	                        	
                        	} 
	                        counter=interval;
                        }
                        displayTime(counter);
                }
                function startQuestionTimer()
                {
                        counter=interval;
                        clearInterval(questionTimer);
                        questionNo=1;
                        questionTimer=setInterval("counting()",1000);
                }
                function pauseQuestionTimer()
                {
                        clearInterval(questionTimer);
                }
                function resumeQuestionTimer()
                {
                        questionTimer=setInterval("counting()",1000);
                }
                function skipToNext()
                {
                        counter=0;
                }
                function counting()
                {
                        counter--;
                }
                function displayTime(secs)
                {
	                 var ones = digits[secs%10];
	                 var tens = digits[(secs - (secs%10))/10];
	                 
	                 document.getElementById('iddate').innerHTML = "<img src='"+tens+"' /><img src='"+spacer+"' /><img src='"+ones+"' />";
                }
				function getNewQuestion()
				{
					questionNo++;
					ajax.get("getQuestion.php?q="+questionNo, showQuestion);
				}
				function showQuestion(result) 
				{
					 document.getElementById('question').innerHTML=result;
				}        
        </script>
	</head>                
    <body onload="init()">
		<table background="digits/clock-back.gif" width=350 height=190>
						<TR>
							<TD align="center">
						<div id="iddate"></div>		
							</TD>
						</tr></table>
			
			<div id="q"><div id="question"></div></div>
					
		
        <div id="data"></div>
        <div id="buttons">
        <input type=button onclick="startQuestionTimer()" value="Start">
        <input type=button onclick="pauseQuestionTimer()" value="Pause">
        <input type=button onclick="resumeQuestionTimer()" value="Resume">
        <input type=button onclick="skipToNext()" value="SkipToNext"></div>
        <div id="reset"><input type=button onclick="parent.location='run1.php?reset=1'" value="Reset questions"></div>

<div id="subject">Cinema and TV</div>

<div id="qnum">
// This is where I need the questionNo value to appear ///////////
</div>


    </body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of Mark Brady
Mark BradyFlag of United States of America imagePrincipal Data Engineer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answers