Php ajax request

Greetings!

I have here simple ajax request


<html>
	<title></title>
	<head>		
		<script type="text/javascript" src="../js/prototype.js"></script>
		<script>

			function sendRequest() {
				new Ajax.Request("test.php",
					{
					method: 'post',
					postBody: 'name='+ $F('name'),
					onComplete: showResponse
					});
				}

			function showResponse(req){
				$('show').innerHTML= req.responseText;
			}
		</script>
	</head>

	<body>
		<form id="test" onsubmit="return false;">
			Name: <input type="text" name="name" id="name" >
			<input type="submit" value="submit" onClick="sendRequest()">
		</form>
		
		<div id="show"></div>
		<br/><br/>
	</body>

</html>

and this is my test.php


<?php
if($_POST["name"] == "")
	echo "name is empty";
else
	echo "you typed ".$_POST["name"];
?>

Its a simple search box.
The code works well, my problem is when I tried to add some spices that when the page is not submitted, I want to display the list of my records inside my database. And when the user hit the the search button, the displayed record will hide and will show the search result.

I tried to fix this using this code:


<?php
if ((isset ($_POST)) {

if($_POST["name"] == "")
	echo "name is empty";
else
	//mysql query
}
else {
..//display records.
}

Any help is really appreciated.

The code works well, my problem is when I tried to add some spices that when the page is not submitted, I want to display the list of my records inside my database.

You mean the first time the page is displayed, before the user uses the form?
Then you’ll have to change the script that creates the initial page as well. Not just the php script called by the AJAX request.

Then you’ll have to change the script that creates the initial page as well. Not just the php script called by the AJAX request.

That’s what I’m thinking but I don’t know how. I thought it’s gonna be the same as the pure php where I can just put value when (!isset($_POST[‘submit’])) in else.