Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts

    need help with submit button using ajax

    Hi
    what i want to do in the following code is when submit button is clicked then send data to php file and display it back again. and if submit button is not clicked then just display data from database which is already present in db.

    posts.js
    Code:
    window.onload = initAll;
    
    function initAll () {
    	
    
     if(submit) {
    	var url = 'posts.php';
    	var queryString  = $('#form1').serialize();
    	$.ajax(
    		   {
    			   type: 'POST',
    			   url: url,
    			   data: queryString,
    			   dataType: 'html',
    			   success: showMessage
    		   });
    	
    	function showMessage(resultData) {
    		$('#message').html(resultData).fadeIn('slow');
    	}
     }
    else {
    	var url = 'posts.php';
    	
    	$.ajax(
    		   {
    			   type: 'POST',
    			   url: url,
    			   dataType: 'html',
    			   success: showMessage
    		   });
    	
    	function showMessage(resultData) {
    		$('#message').html(resultData).fadeIn('slow');
    	}
    }
    }
    posts.hmtl
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="lib/jquery-1.4.min(Production).js"></script>
    <script type="text/javascript" src="posts.js"></script>
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <form id="form1" name="form1" method="post" action="">
      <p>
        <label>comment
          <textarea name="post" id="post" cols="45" rows="5"></textarea>
        </label>
      </p>
      <p>
        <label>
          <input type="button" name="submit" id="post" value="Submit" onclick="posts()" />
        </label>
      </p>
    </form>
    </body>
    <div id="message"></div>
    </html>

  • #2
    New to the CF scene
    Join Date
    Feb 2010
    Location
    CPU
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are you using any debugging tool ?


  •  

    Posting Permissions

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