user982853
user982853

Reputation: 2488

jQuery Ajax = success return page

Ok so I know long hand ajax but trying to use the jQuery short cut. I have two documents

  1. form.php
  2. submit.php

In my "form" page I am calling the "submit" page to process the insert. I am currently using the jquery ajax:

   <script type="text/javascript"> 
   jQuery('form').submit(function() {   
   string = jQuery("form").serializeArray();
   jQuery.ajax({
   type: "POST",
   url: "submit.php", 
   data: string,
   dataType: "json",
   })   
   return false; 
   }); 
   </script>

When I view firebug it is processing the ajax fine. I am getting 200 and post parameters are set. What I am trying to do is have the ajax return the submit.php file. I know it has something to do with the "success" function but I don't know how to add this. I tried a few things like:

   <script type="text/javascript"> 
   jQuery('form').submit(function() {   
   string = jQuery("form").serializeArray();
   jQuery.ajax({
   type: "POST",
   url: "submit.php", 
   data: string,
   dataType: "json",
   success: function(html){
   alert(html);
   }
   })   
   return false; 
   }); 
   </script>

and

   <script type="text/javascript"> 
   jQuery('form').submit(function() {   
   string = jQuery("form").serializeArray();
   jQuery.ajax({
   type: "POST",
   url: "submit.php", 
   data: string,
   dataType: "json",
   success: function(html){
   $('.result').html(data);
   }
   })   
   return false; 
   }); 
   </script>

but neither of these are working. Again I am simply trying to send the ajax request and then return the contents of the submit.php page. Not only does the submit.php page hold the script to process the php/ajax insert but it also display success statements like "insert was successful" so that is why I need to not only run the script in the page but also return the contents of that page. Thank you for any help.

Upvotes: 0

Views: 6167

Answers (2)

Alex
Alex

Reputation: 35399

Chagne the dataType:'json' to dataType:'html' for the callback that you wish to display the contents of submit.php.

Upvotes: 2

Kevin Ji
Kevin Ji

Reputation: 10489

You were close in your second attempt, but you made a typo. Try:

success: function (data) {
    $('.result').html(data);
}

Also, unless your server is returning JSON, you probably want to change the dataType:

dataType: "html"

Upvotes: 1

Related Questions