How can I get form data with JavaScript/jQuery?

前端 未结 28 2201
不知归路
不知归路 2020-11-22 12:39

Is there a simple, one-line way to get the data of a form as it would be if it was to be submitted in the classic HTML-only way?

For example:



        
28条回答
  •  -上瘾入骨i
    2020-11-22 13:26

    showing form input element fields and input file to submit your form without page refresh and grab all values with file include in it here it is

    on Submit button page will send ajax request to your php file.
    $('#imageUploadForm').on('submit',(function(e) 
    {
         fname = $('#fname').val();
         lname =  $('#lname').val();
         address =  $('#address').val();
         phoneno =  $('#phoneno').val();
         file =  $('#file').val();
         e.preventDefault();
         var formData = new FormData(this);
         formData.append('file', $('#file')[0]);
         formData.append('fname',$('#fname').val());
         formData.append('lname',$('#lname').val());
         formData.append('phoneno',$('#phoneno').val());
         formData.append('address',$('#address').val());
         $.ajax({
    		type:'POST',
                    url: "test.php",
                    //url: '',
    
                    data:formData,
                    cache:false,
                    contentType: false,
                    processData: false,
                    success:function(data)
                    {
                         alert('Data with file are submitted !');
    
                    }
    
         });
    
    }))

提交回复
热议问题