Javascript Ajax表单数据

Javascript Ajax表单数据,javascript,php,ajax,Javascript,Php,Ajax,我有这段代码,它告诉我如何将表单值发送到php文件 <form role="form" id="upload_form" method="post" enctype="multipart/form-data"> <div class="form-group"> <label for="formlabel">Title</label> <input class="form-control" name="titl

我有这段代码,它告诉我如何将表单值发送到php文件

 <form role="form" id="upload_form" method="post" enctype="multipart/form-data">

     <div class="form-group">
     <label for="formlabel">Title</label>
     <input class="form-control" name="title" id="title" placeholder="Enter Software Name" type="text" value="<?php if(isset($title)){echo $title;}?>" required>
     </div>

    <div class="form-group">
    <label>Short Meta Description atleast 155 words</label>
    <textarea class="form-control" name="shortdec" id="shortdec" rows="3" required><?php if(isset($shortdec)){echo $shortdec;}?></textarea>
</div>


    <div class="form-group">
    <label>File input</label>
    <input name="softpost" id="softpost" type="file" required>
</div>


     <input type="button" value="Upload File" onclick="uploadFile()">
      <progress id="progressBar" value="0" max="100" style="width:300px;"></progress>
      <h3 id="status"></h3>

如果我没弄错的话,你想发送带有文件的“普通”东西

嗯,这非常简单:

formdata = new Formdata();
formdata.append('softpost', file)
formdata.append('name', 'My super file')
就这样

马特

你可以用

Jquery

$("form#upload_form").submit(function(){
    //Fetch Form Data i.e it will include all the form elements including file and other form inputs
    var formData = new FormData($(this)[0]);
    .........AJAX CALL...........
    return false;
});
JavaScript

var form = document.getElementById("upload_form");
var formData = new FormData(form);

只需使用
newformdata($(this)[0])
获取表单字段并继续进行普通的AJAX调用。您现在可以在后端php代码中获取标题、文件和描述元素。

我们需要javscript的可能副本,如上文所述。如何使用jquery$('#inputid').val()从html输入字段获取名称值我们需要关于javascriptdocument.getElementById('inputid')).value也有同样的作用
var form = document.getElementById("upload_form");
var formData = new FormData(form);