Avatar of Mark Brady
Mark Brady
Flag for United States of America asked on

How to use a global variable outside the script tag

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.
$reset = $_GET['reset'];
if($reset == 1) include("reset.php");

<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/9.gif" ];
				var spacer = ["digits/spacer.gif"];
                function init()
                function checkCounter()
                        if (counter==0)
                            if (questionNo<10) 

	                        	document.getElementById('question').innerHTML='All Done !';
                                        clearInterval(questionTimer); // Stop the timer
                function startQuestionTimer()
                function pauseQuestionTimer()
                function resumeQuestionTimer()
                function skipToNext()
                function counting()
                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()
					ajax.get("getQuestion.php?q="+questionNo, showQuestion);
				function showQuestion(result) 
    <body onload="init()">
		<table background="digits/clock-back.gif" width=350 height=190>
							<TD align="center">
						<div id="iddate"></div>		
			<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 ///////////


Open in new window


Avatar of undefined
Last Comment
Mark Brady

8/22/2022 - Mon
Mark Brady

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck