SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    php ajax request

    Greetings!

    I have here simple ajax request
    Code:
    <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 Code:
    <?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 Code:
    <?php
    if ((isset ($_POST)) {

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

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    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.

  3. #3
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •