Click here to Skip to main content
15,886,035 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
when i click on submit, i get error that undefined index fname.
please resolve or guide me.

this is code of my html file
XML
<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>


XML
<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>



and this is code of my php file
PHP
<?php
echo $_REQUEST['fname'];
?>
Posted
Updated 9-Jul-15 8:36am
v2
Comments
Sergey Alexandrovich Kryukov 9-Jul-15 15:22pm    
If you did not use the handler of the Submit button at all, the form data could be submitted by a form itself. Why trying to double the submission?
—SA
CHITRAKSH 2010 9-Jul-15 23:19pm    
i want to do it using ajax, further i want that both textbox and image file can be uploaded by ajax.do you have any suggestion?
Sergey Alexandrovich Kryukov 9-Jul-15 23:39pm    
Why? You can do it all with the form along. And if this is Ajax, why using a form? What suggestions? Just do it. What is unclear?
By the way, on PHP site, you will face huge security problem. How are you going to use these two records?
—SA
Prava-MFS 22-Jul-15 2:27am    
Image file (form type enctype) can not be submitted through AJAX. You need to use iframe for posting the data easily. But, if you want to get the fname, lname, email only in the $_REQUEST content, then remove this line `contentType: false,` and then try. You will get $_REQUEST['fname'], $_REQUEST['lname'] and $_REQUEST['email'] value.

Note: Best to use $_POST instead of $_REQUEST.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900