【问题标题】:Problem with moving file and saving file data blank移动文件并将文件数据保存为空白的问题
【发布时间】:2013-10-16 12:26:18
【问题描述】:

我这里有点问题..

我使用 index.php 上的上传,并使用 jquery (ajax) 将任何信息保存到 mysql。我有 3 个文件 index.php、savedata.php、jsSave.js

但是当我使用 savedata.php 中的 $_REQUEST 文件并且结果在 mysql 表中也是空白响应时,我替换 $_REQUEST 并使用 $_FILES 并获得相同的结果。

我已经尝试使用的代码如下...

索引.php

        <form class="myform" action="<?php $_SERVER['PHP_SELF'];?>" method="POST" name="myform" id="myform" enctype="multipart/form-data" style="width:350px;">

                            <li>
                            <label>Item Picture</label>
                            <div class="fileUpload btn btn-primary">
                                <span>Choose Image..</span>
                                <input type="file" name="cItemPicture" class="cItemPicture" id="cItemPicture"/>
                            </div>
                            <input type="hidden" name="cPic" id="cPic"/>
                        </li>

                        <li>
                            <img border="0" src="images/loading_transparent.gif" width="20" height="20" id="imgLoad">
                        &nbsp<button class="button_blue" id="butTblSave" type="submit" style="width: 81px; height: 33px">SAVE</button>
                        </li>

对于 savedata.php 文件的脚本是

    <?php 
   if($_REQUEST)
{
    ***$cItemPicture=$_FILES["cItemPicture"]["name"];***

    $sql="INSERT INTO tblData(item_image) VALUES ('$cItemPicture')";

    $result=mysql_query($sql) or die('Cannot Connect To SQL Server, Please contact your administrator');
      move_uploaded_file($_FILES["cItemPicture"]["tmp_name"],
      "upload/" . $_FILES["cItemPicture"]["name"]);
}
?>

最后一个使用 jQuery 文件作为 AJAX 工作的文件是 jsSave.js

    $(document).ready(function() {
    $('#imgLoad').hide();
    $('#msgConfirm').hide();
    $('#tblAvailabilityResult').hide();
});

$(function() {  
  $("#butTblSave").click(function() {  
        $.ajax({
        type: 'POST',
        url: 'saveData.php',
        data: $('form').serialize(),

            beforeSend: function() {
                $("imgLoad").show();
            },

            complete: function() {
                $("imgLoad").hide();
            },        

            cache: false,

            success: function () {

                $("#cItemPicture").val('');

                $('#imgLoad').hide();
                $('#butTblSave').attr("disabled", false);

                $('#msgConfirm').fadeIn(500).delay(5000).fadeOut(1000);
                $("#msgConfirm").html(' Add New Item Success.');

            }

        });
        return false;
    }

  });  
});  

我想念什么吗?当按下 SAVE 按钮时,ajax 响应空白并将数据保存到 mysql item_image 中也是空白的,也没有文件移动到上传文件夹中。

对这个问题有什么想法吗?非常感谢。

谢谢

【问题讨论】:

    标签: php jquery mysql ajax upload


    【解决方案1】:

    尝试阻止表单的默认提交。 因为表单是提交给SELF(index.php)

    $("#butTblSave").click(function(event) { 
    event.preventDefault();
    ...
    

    【讨论】:

    • 我的意思是,如何在 savedata.php 中获取文件上传值?使用 $_FILES["cItemPicture"]["name"];我尝试使用 $_REQUEST 仍然没有任何反应
    猜你喜欢
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多