jquery - ajax is not sending data to php -


i want send formdata using ajax php file when click on button there error undefined index fname.

<form id="myform" method ="post" enctype="multipart/form-data">     first name: <input type="text" name="fname" id="fname" /> <br>     last name: <input type="text" name="lname" id="lname" /> <br>     email:  <input type="text" name="email" id="email" /> <br>     image: <input type="file" name="image" id="image" /> <br>     <button type="button" name="btnsubmit" id="btnsubmit"> submit </button> </form>  <script language="javascript" src="js/jquery-1.9.1.min.js"></script> <script language="javascript" src="js/jquery.form.js"></script> <script type="text/javascript">     $(document).ready(function(){     //alert(4);     $("#btnsubmit").click(function(){          var formdata = new formdata($("#myform"));          alert(formdata);          console.log(formdata);          $.ajax({             type: 'post',             url: 'learn_form2.php',             data: formdata,              success: function (data) {                alert(data);              },             cache: false,             contenttype: false,             processdata: false           });       });      }); </script> 
<?php     echo $_request['fname']; ?> 

please help

note formdata expects form domelement, not jquery object. note should bind submit event of form itself, not click of submit button. allows prevent standard form submission. try this:

$("#myform").submit(function(e) {      e.preventdefault(); // stop standard form submission       var formdata = new formdata(this); // give form domelement formdata      $.ajax({          type: 'post',          url: 'learn_form2.php',          data: formdata,          success: function (data) {              alert(data);          },          cache: false,          processdata: false     }); });  

Comments