如何使用文件通过ajax jquery post方法工作

时间:2017-03-29 11:58:22

标签: php jquery ajax

如何通过jQuery post ajax方法在Php上传图像?我以简单的方式尝试了这段代码,但它没有显示在文件数据中,也没有存储在目录中,即使它存储在数据库中也是如此。它的所有数据通过表格

        <form  id="Myform5" method="POST" action="" 
       enctype="multipart/form-data">
                              <label >Class</label>
                           <select name="type" id="Class">
                           <option >Choose option</option>
                           <option value="Matric">Matric</option>
                            </select>
                             <input  type="text" id="Institute_Name" >
                                 <label >Institute Name</label>
                            <input type="text"  id="Board_Name">
                            <label  >Board Name</label>
                            <div class="form-group">
                            <label>Starting Year</label>
                         <select name="type" id="Starting_Year" >
                              <option >Select year</option>
                              <option value="1950">1950</option>
                         </select>

                                <label>Ending Year</label>
                                <select name="type" id="Ending_Year" >
                               <option >Select year</option>
                               <option value="1950">1950</option>
                                </select>

                     <input  type="text" id="Total_Marks" >
                         <label>Total Marks</label>
                  <input  type="text" id="Obtained_Marks" >
                  <label >Obtained Marks</label>

                                <label>Result_Card</label>
                      <input type="file" id="Result_Card" 
                    id="file"  class="inputbox" placeholder="Default Input">

                      <textarea  id="Awards_Honors" type="text" ></textarea>
                              <label >Awards & Honors</label>
                  <textarea  id="Co-curricular" type="text"  ></textarea>
                              <label >Co-curricular</label>
                   <textarea   id="Additional_Info" type="text" ></textarea>
                              <label >Additional Info</label>
                      <button  type="button"  id="button4" >

其juery ajax post

       $("#button4").click(function(){
  var Class = $("#Class").val();
  var Institute_Name =$("#Institute_Name").val();
  var Board_Name=$("#Board_Name").val();
  var Starting_Year = $("#Starting_Year").val();
  var Ending_Year =$("#Ending_Year").val();
   var Total_Marks=$("#Total_Marks").val();
   var Obtained_Marks = $("#Obtained_Marks").val();
  var Result_Card =$("#Result_Card").val();
  var filename = Result_Card.replace(/^.*\\/, "");
   console.log(filename);
  var Awards_Honors=$("#Awards_Honors").val();
  var Co_curricular = $("#Co-curricular").val();
   var Additional_Info =$("#Additional_Info").val();

if (Class.length == "" || Institute_Name.length =="") {
   alert("Fill the form");
}
else
{
    $.ajax({
        type:'POST',
        url :'view2.php',

        data : {
            Class:Class,
            Institute_Name:Institute_Name,
            Board_Name:Board_Name,
            Starting_Year:Starting_Year,
            Ending_Year:Ending_Year,
            Total_Marks:Total_Marks,
            Obtained_Marks:Obtained_Marks,
            filename:filename,
            Awards_Honors:Awards_Honors,
            Co_curricular:Co_curricular,
            Additional_Info:Additional_Info
        },
        // cache: false,
        // contentType: false,
         enctype: 'multipart/form-data',
        // processData: false,
        // enctype:'application/x-www-form-urlencoded',
        success : function(feedback){
         console.log(feedback);
          alert("sucess");
            $("#Myform5")[0].reset();
        }

    })
 }});

现在我给这个页面的php

  if (isset($_POST['Class']) && 
          isset($_POST['Institute_Name']) &&    isset($_POST['Board_Name']) 
      && isset($_POST['Starting_Year']) && isset($_POST['Ending_Year'])  
        && isset($_POST['Total_Marks']) &&  isset($_POST['Obtained_Marks'])
          &&  isset($_POST['filename']) && isset($_POST['Awards_Honors']) &&
      isset($_POST['Co_curricular']) ){
     /*Uploading FIle OF Image or other in jpg jpeg png size 2 MB */
                                         $errors= array();
                            $file_name = $_FILES['filename']['name'];
                            $file_tmp =$_FILES['filename']['tmp_name'];
                            $file_type=$_FILES['filename']['type'];   

   $file_ext=strtolower(end(explode('.',$_FILES['filename']['name'])));    

    $expensions=   array("jpeg","jpg","png","txt","pdf","docx","bmp",
                                          "gif","jpeg","jpg");     

    if(in_array($file_ext,$expensions)=== false){
            $errors[]="extension not allowed, please choose a 
      JPEG,txt,pdf,docx,bmp,gif,jpeg,jpg or PNG file.";
                                        }
      if(empty($errors)==true){

      move_uploaded_file($file_tmp,"files/".$file_name);
                  echo "Image Uploaded Successfully";     
                                        }else{
                                            print_r($errors);
                                        }echo"<br />";
                 $Class = $_POST['Class'];
                 $Institute_Name= $_POST['Institute_Name'];
                 $Board_Name = $_POST['Board_Name'];
                 $Starting_Year= $_POST['Starting_Year'];
                 $Ending_Year = $_POST['Ending_Year'];
                 $Total_Marks= $_POST['Total_Marks'];
                 $Obtained_Marks = $_POST['Obtained_Marks'];
                 $filename = $_POST['filename'];
                 $Awards_Honors = $_POST['Awards_Honors'];
                 $Co_curricular = $_POST['Co_curricular']; 
                 $Additional_Info = $_POST['Additional_Info'];                     
               $sql=" INSERT INTO `secondary level` 
               (`Class`,`Institute_Name`, `Board_Name`, `Starting_Year`,
              `Ending_Year`, `Total_Marks`, `Obtained_Marks`, `Result_Card`,
                `Awards_Honors`, `Co-curricular`, `Additional_Info`) VALUES
                ( '$Class', '$Institute_Name', '$Board_Name', 
                  '$Starting_Year', '$Ending_Year', '$Total_Marks',
              '$Obtained_Marks','$filename','$Awards_Honors',
                '$Co_curricular ','$Additional_Info' )";

          if ($conn->query($sql) === TRUE) {
          echo "New record created successfully";
        } else {
       echo "Error: " . $sql . "<br>" . $conn->error;
       }}

1 个答案:

答案 0 :(得分:0)

只需为表单中的每个元素提供name属性,然后更改输入类型按钮以提交,然后使用:

$("#Myform5").submit(function(a){
    a.preventDefault();
    var formData = new FormData(this);
   $.ajax({
                type:'POST',
                url: 'view2.php',
                data:formData,
                cache:false,
                success:function(data){
                console.log(data);
                location.reload();
                },
                error: function(data){
                console.log(data);
                }
            });

});

这可能会在不在本地计算机上发布文本数据的情况下上传图像,但它可以在Web服务器上运行。