在jquery ajax中成功上传数据后,无法打印成功消息

萨姆帕斯

我是Jquery,Ajax和php的新手,编写了一个程序,将文件作为blob文件上传到数据库,我成功将文件上传到数据库。但是这里有一个问题,<div id="msg"></div>在提交文件后,现在我无法在该部分中打印成功消息我尝试了stackoverflow中给出的其他方式和问题,但没有一个对我有用。文件“ index.php

包含用于ajax和文件上传的代码,其中up​​load.php包含用于将文件插入数据库的逻辑。

index.php

<?php include_once 'dbconfig.php'; ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>File upload</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $("#button").click(function() {
                var formData = new FormData();
                formData.append('file', $('#file')[0].files[0]);
                $.ajax({
                    url : 'upload.php',
                    type : 'POST',
                    data : formData,
                    processData: false,  
                    contentType: false, 
                    success : function(data) {
                        //console.log(data);
                        document.getElementbyID("msg").innerHTML()="";
                        alert(data);
                    }
                });
            });
        });
    </script>
</head>
<body>
    <div id="header">
        <tt>Upload file</tt>
    </div>
    <div id="msg"></div>
    <form id="form" name="form" enctype="multipart/form-data">
        <input type="file" name="file" id="file" />
        <button id="button" name="button"/>Submit</button>
    </form>
</body>
</html>

upload.php

<?php
    include_once 'dbconfig.php';

    $file = rand(1000, 100000)."-".$_FILES['file']['name'];
    $file_loc = $_FILES['file']['tmp_name'];
    $file_size = $_FILES['file']['size'];
    $file_type = $_FILES['file']['type'];
    $folder = "upload/";

    // new file size in KiloByte
    $new_size = $file_size / 1024;
    // make file name in lower case
    $new_file_name = strtolower($file);
    $final_file=str_replace(' ', '-', $new_file_name);

    if (move_uploaded_file($file_loc, $folder.$final_file)) {
        $sql = "INSERT INTO files(file) VALUES('$final_file')";
        mysqli_query($connection, $sql);
        mysqli_close($connection);
    }
?>
Sam4Code

您必须先将响应打印到php中。在行print "File uploaded successfully!!";添加mysqli_close($connection);jQuery代码似乎是正确的。

<?php
include_once 'dbconfig.php';

try {
         $file = rand(1000,100000)."-".$_FILES['file']['name'];
         $file_loc = $_FILES['file']['tmp_name'];
         $file_size = $_FILES['file']['size'];
         $file_type = $_FILES['file']['type'];
         $folder="upload/";

         // new file size in KiloByte
         $new_size = $file_size/1024;
         // make file name in lower case
         $new_file_name = strtolower($file);
         $final_file=str_replace(' ','-',$new_file_name);

         if(move_uploaded_file($file_loc,$folder.$final_file))
         {
              $sql="INSERT INTO files(file) VALUES('$final_file')";  
              mysqli_query($connection,$sql);
                        
              print "success";
         }
         else
        {
          print "error";
         }
  } catch ( Exception $e ) {
    print "error";
    }finally {
   mysqli_close($connection);  
  }
?>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法使用Jquery Ajax打印成功消息

来自分类Dev

在jQuery Ajax中突出显示成功后的数据

来自分类Dev

jQuery ajax成功$(this)后无法正常工作

来自分类Dev

jQuery ajax成功$(this)后无法正常工作

来自分类Dev

无法从php接收到Ajax,jQuery的成功消息

来自分类Dev

jQuery中的Ajax文件上传返回成功结果

来自分类Dev

jQuery ajax返回成功数据

来自分类Dev

jQuery ajax返回成功数据

来自分类Dev

jQuery Ajax文件上传完成功能

来自分类Dev

ajax成功后的jQuery addClass / removeClass

来自分类Dev

成功的jQuery Ajax调用无法异步处理

来自分类Dev

jQuery / Ajax-成功消息后重定向到另一个页面

来自分类Dev

jQuery / Ajax-成功消息后重定向到另一个页面

来自分类Dev

jQuery ajax成功范围

来自分类Dev

拒绝$ .ajax成功方法中的jQuery Promise

来自分类Dev

如果AJAX成功/失败后,jQuery无法在内部运行

来自分类Dev

jQuery Ajax 输入文本和文件处理到成功函数后无法获取值

来自分类Dev

jQuery验证显示成功消息

来自分类Dev

带有ajax / jquery请求的html页面中未显示成功消息

来自分类Dev

jQuery / Ajax返回成功消息并显示“已保存”指示符

来自分类Dev

jQuery Ajax在成功之前取消或取消成功

来自分类Dev

在asp.net mvc 5中使用jQuery成功调用ajax后提交表单

来自分类Dev

jQuery验证成功后使用ajax提交表单

来自分类Dev

成功返回jQuery ajax后,继续每个迭代的工作

来自分类Dev

使用Ajax / Jquery成功提交后如何隐藏表单

来自分类Dev

当ajax响应成功时,清除jQuery成功/错误消息跨度类

来自分类Dev

jQuery在Ajax上成功绑定

来自分类Dev

jquery ajax参数成功

来自分类Dev

jQuery Ajax失败...随机成功

Related 相关文章

热门标签

归档