Refreshing a div tag to change results

Hello,
I found a script that refreshes a div tag but it only works half way. I have a php if statement in the middle of the script and based on the if decision it should display the appropriate information.
if no click display " button not pressed"
else
echo random number

the first part works but not the sec. part.
any ideas as to how i can fix this.?


<html>
	<head>
		<meta charset="ISO-8859-1">
		<script src="http://code.jquery.com/jquery-latest.js"></script>
		<script>
		$(document).ready(function() {
			$('#btn_click').on('click', function() {
				var url = 'refreshDiv.php';
				$('#div1-wrapper').load(url + ' #div1');
			});
		});
		</script>
	</head>
<body>
<?php echo rand(10,1000); // test outside the div container?>
	
	<div id="div1-wrapper">
		<div id="div1"  style="border:solid 2px red; width: 200px;">
			<form name="form1" method="POST">
				<?php
					if(empty($_POST["btn_click"]))
					{ 	echo 'button not pressed'; }
					else
					 {	echo rand(10,1000); } 			
				?>				
		</div>
	</div>	
	 <button type="button"  id="btn_click" /> click me! </button> 			
	</form>	
	</body>
</html>

Currently you are checking if the form has been posted:


if(empty($_POST["btn_click"]))

But instead of posting the form, you are loading a new page.


$('#div1-wrapper').load(url + ' #div1');

So don’t do that. Get rid of all the scripting - you don’t need it for what you’re doing here.

Instead of scripting, give the button a name and a value so that the empty() check will find something, and things will work as desired.


<button type="submit"  name="btn_click" value="submitted" /> click me! </button>           

Alternatively, you can get away with no value by using another technique to check if btn_click exists, by using filter_has_var, that lets you easily check for such things:


if(!filter_has_var(INPUT_POST, "btn_click"))


<button type="submit"  name="btn_click" /> click me! </button>