Jump to content

execute php code if javascript number = 0


matthew9090

Recommended Posts

i've got this code:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
	  var number = 10;
	  var url = "index.php?number=0";
	  function countdown() {
				setTimeout(countdown, 1000);
				$('#box').html("Selecting person in " + number + " seconds");
				number --;

				if (number<0) {
					window.location = url;
					number = 0;
				}
	  }
	});
</script>
<center>

<div id="box">
<?php
error_reporting(0);
$file = file('names.txt');
$rand_line = rand(0, sizeof($file) - 1);
$line = $file[$rand_line];
if ($_GET['number']=="0")
{
echo $line;
}
else 
{
?>
<script type="text/javascript">
countdown();
</script>
<?php
}
?>
</div>

 

it is meant to when the javascript number = 0 show a random line from file but it comes up with undefined index 'number' on line 28

Link to comment
Share on other sites

now ive tryed another way which works but then won't stop showing names once the timer has finished

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
	  var number = 2;
	  function countdown() {
				setTimeout(countdown, 1000);
				$('#box').html("Selecting person in " + number + " seconds");
				number --;

				if (number<0) {
				keywords = 
				[
				"name1", 
				"name2",
				"name3",
				"name4",
				"name5"
				]
					var keyword = keywords[Math.floor(Math.random()*keywords.length)]
					document.write(keyword);
					number = 0;
				}
	  }
	  countdown();
	});
</script>
<center>
<div id="box">
</div>
</center>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.