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
https://forums.phpfreaks.com/topic/232844-execute-php-code-if-javascript-number-0/
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>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.