PHP无法上传图片而无法看到$ image值。所有其他工作,它是关于ajax,没有刷新形式

时间:2018-05-10 14:53:12

标签: javascript php mysql ajax

HTML表单没有刷新插入数据库,我没有复制完整的代码,其他输入元素工作,只有$ image不插入数据库也没有上传目录。没有ajax功能一切正常,但表格有重载,我想没有重装,如果有人可以帮我,我告诉你谢谢你。

 html
 <form id="formid" method="post" action="phppath/sucms.php" 
  onsubmit="return fsocietyApp()" enctype="multipart/form-data">
  <div id="img" class="f-col s12 m6 l6">
   <label for="uploadphoto">Upload picture</label>
   <input type="file" name="uploadphoto" id="uploadphoto">  
   <span id="fsoc" style="float:right;"></span>
  </div>
  </form>



javascript
functuin fsocietyApp(){
 if(document.getElementById("uploadphoto").files.length != 0 && ex != 
 "gif" && ex != "png" && ex != "bmp" && ex != "jpeg" && ex != "jpg") 
  {
    error.innerHTML = "<img style='position:relative;bottom:12px' 
  src='img/red.png' alt='error'>";
    return false;
  }
  $.ajax({
  type: 'POST',
  url: 'phppath/sucms.php',
  data: $('#formid').serialize(),
  success: function(response){
   $('#success').html(response);
  }
 });
 var form = document.getElementById('formid').reset();
 error.innerHTML = "";
 return false; 
  }

 php
 $image = $_FILES["uploadphoto"]["name"];
 if($role == 'Admin' || $role == 'Author'){
    if(!empty($fname)){
        $q = "INSERT INTO `fusr` (`uid`, `udate`, `urole`, `uimg`, 
     `ufname`, `ulname`, `usrname`, `uemail`, `upsw`, `uviews`, 
      `udetails`) VALUES (NULL, '$date', '$role', '$image', '$fname', 
      '$lname', '$uname', '$email', '$password', '0', '$details')";
        if(mysqli_query($c, $q)){
            echo "hi";
        }
       }
     }

2 个答案:

答案 0 :(得分:2)

Chang从serialize()到新的FormData()检查如下:

 var formData = new FormData(this);
$.ajax({
  type: 'POST',
  url: 'phppath/sucms.php',
  data: formData ,
  success: function(response){
   $('#success').html(response);
  }
 });

答案 1 :(得分:-2)

从您的代码中纠正函数fsocietyApp

的定义

&#13;
&#13;
{{1}}
&#13;
&#13;
&#13;