猿问

如何使用 php 和 ajax 使用函数插入文件和其他输入类型

我正在尝试使用 ajax 将文件和文本插入数据库,但它不起作用。主要问题是文件未插入数据库这是我的代码


     <form id="descriptionsubmit" enctype = "multipart/form-data">

    <textarea class="form-control" id="textarea-description" placeholder="Write Something"></textarea>

  <input type="file" name="upload-pic" id="upload-pic" class="inputfile" >  

 <button type="submit" class="update-btn">Next</button>

</form>

Ajax 同样的警报(数据)没有显示任何内容


$(function(){

    

        $('form#descriptionsubmit').on('submit', function (e) {

          e.preventDefault();

          var file_data = $('#upload-pic').prop('files')[0];

    var formData = new FormData(this);

    $.ajax({


         method: "POST",

        url : "AJAXSubmitClientData.php", 

       

        data :formData,

        dataType : "html",

                 

            contentType: false,

             enctype: 'multipart/form-data',

            cache: false,

            processData:false,

        success:function(data)

        {

            alert(data);

        }

    });

PHP代码这里插入了描述但没有插入文件


if(isset($_POST['formData']))

{


$file = array(   

    "name" => $_FILES['form_data']['name'],

    "tmp_name" => $_FILES['form_data']['tmp_name']   

    );

    print_r($file);

$fileinserted = $objMaster->imageinsert($file);

$dataDescription = array(

    "description"           => $_POST['description'],

    "projectProfile" => $fileinserted,

    );

  

 echo $insertdata = $objMaster->updateJobPostTable($dataDescription,$_SESSION['lastinsertid']);

echo 1;

}

图像插入函数


 public function imageinsert($file, $path = "") {

        $fname = "";

        $uploadpath = "";

        if ($path == "") {

            $uploadpath = $this->upload1;

        } else {

            $uploadpath = $this->upload1 . $path . "/";

        }

        $current_timestamp = $this->timeStamp();

        $filename = basename($file['name']);

        $newname = $current_timestamp . $filename;

        if ((move_uploaded_file($file['tmp_name'], $uploadpath . $newname))) {

            $fname = $newname;

        }

        return $fname;

    }

任何帮助表示赞赏


烙印99
浏览 94回答 1
1回答

临摹微笑

HTML 页面 :-将button属性值更改为type="button"而不是type="submit"&添加onclick事件属性。&nbsp;<form id="descriptionsubmit" action="" method="post" enctype = "multipart/form-data">&nbsp; &nbsp; <textarea class="form-control" name="textarea-description" placeholder="Write Something"></textarea>&nbsp; <input type="file" name="upload-pic" id="upload-pic" class="inputfile" >&nbsp;&nbsp;&nbsp;<button type="button" onclick="addData()" class="update-btn">Next</button></form>JS AJax 代码:-function addData() {&nbsp; &nbsp; var formData = new FormData($('#descriptionsubmit')[0]);&nbsp; &nbsp; formData.append('action', 'add');&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; method: 'post',&nbsp; &nbsp; &nbsp; &nbsp; processData: false,&nbsp; &nbsp; &nbsp; &nbsp; contentType: false,&nbsp; &nbsp; &nbsp; &nbsp; cache: false,&nbsp; &nbsp; &nbsp; &nbsp; enctype: 'multipart/form-data',&nbsp; &nbsp; &nbsp; &nbsp; url: 'AJAXSubmitClientData.php',&nbsp; &nbsp; &nbsp; &nbsp; data: formData,&nbsp; &nbsp; &nbsp; &nbsp; success:function(msg){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(msg);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}AJAXSubmitClientData.php<?phpinclude('dbconn.php');if(isset($_POST["action"]) && $_POST["action"]=="add"){&nbsp; echo&nbsp; $a = $_POST["textarea-description"];&nbsp; &nbsp;echo $g = $_FILES["upload-pic"]["name"];// insert code here.&nbsp;}?>
随时随地看视频慕课网APP
我要回答