×

php up to date

Multi Form Submit using Ajax and Php

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>


<form name="multiform" id="multiform" action="multi-form-submit.php" method="POST" enctype="multipart/form-data">
Name: <input type="text" name="name"  value="manikandan"/> <br/>
Age :<input type="text" name="age"  value="1" /> <br/>
Image :<input type="file" name="photo" /><br/>
</form>

<input  type="button" id="multi-post" value="Run Code"></input>
<div id="multi-msg"></div>

<script>
$(document).ready(function()
{


 function getDoc(frame) {
     var doc = null;
   
     // IE8 cascading access check
     try {
         if (frame.contentWindow) {
             doc = frame.contentWindow.document;
         }
     } catch(err) {
     }

     if (doc) { // successful getting content
         return doc;
     }

     try { // simply checking may throw in ie8 under ssl or mismatched protocol
         doc = frame.contentDocument ? frame.contentDocument : frame.document;
     } catch(err) {
         // last attempt
         doc = frame.document;
     }
     return doc;
 }

$("#multiform").submit(function(e)
{
$("#multi-msg").html("<img src='loading.gif'/>");

var formObj = $(this);
var formURL = formObj.attr("action");

if(window.FormData !== undefined)  // for HTML5 browsers
// if(false)
{

var formData = new FormData(this);
$.ajax({
        url: formURL,
       type: 'POST',
data:  formData,
mimeType:"multipart/form-data",
contentType: false,
       cache: false,
        processData:false,
success: function(data, textStatus, jqXHR)
   {
$("#multi-msg").html('<pre><code>'+data+'</code></pre>');
   },
  error: function(jqXHR, textStatus, errorThrown)
    {
$("#multi-msg").html('<pre><code class="prettyprint">AJAX Request Failed<br/> textStatus='+textStatus+', errorThrown='+errorThrown+'</code></pre>');
    }      
  });
        e.preventDefault();
        e.unbind();
   }
   else  //for olden browsers
{
//generate a random id
var  iframeId = 'unique' + (new Date().getTime());

//create an empty iframe
var iframe = $('<iframe src="javascript:false;" name="'+iframeId+'" />');

//hide it
iframe.hide();

//set form target to iframe
formObj.attr('target',iframeId);

//Add iframe to body
iframe.appendTo('body');
iframe.load(function(e)
{
var doc = getDoc(iframe[0]);
var docRoot = doc.body ? doc.body : doc.documentElement;
var data = docRoot.innerHTML;
$("#multi-msg").html('<pre><code>'+data+'</code></pre>');
});

}

});


$("#multi-post").click(function()
{

$("#multiform").submit();

});

});
</script>

</body>
</html>
SHARE

blogger

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

Thanks for your valuable feedback