照片没有存储到数据库中。 php [undefined索引]

时间:2014-02-09 19:07:58

标签: php ajax

我有这样的形式:

<form class="wrefresh" action="Code Files/Accused.php" method="post" enctype="multipart/form-data">

<div class="row-form">
    <div class="span3" style="margin-top: 06px">Picture:</div>

     <div id="photo_settings2" style="margin-left:11px;">
         <img id="Picture2" src="../../img/User No-Frame.png"/>       
     </div>

    <div id='Upload_Panel'>
       <input name="file" type='file' id='file_browse' onchange="readURL(this,'Picture2')"    style="cursor: pointer;"/>
    </div>
</div> 

<button type="submit" class="a-btn2"style="cursor: pointer; background-color:#5C635F; color:white; font-family:'Candara'; margin-top:-47px; margin-left:192px; height:37px;"><big>Submit</big></button>

</form>

我在Accused.php中有一个PHP代码:

$Mother_Language           =         $_POST['mlang'];
$Other_Languages           =         $_POST['olang'];
$Complexion                =         $_POST['comp'];
$Previous_Thug_Record      =         $_POST['precord'];
$Religion                  =         $_POST['religion'];
$Type_of_Accused           =         $_POST['taccused'];              
$City                      =         $_POST['city'];
$Country                   =         $_POST['country'];
$Nationality               =         $_POST['nationality'];
$ID_Mark                   =         $_POST['idmark'];
$Hair_Color                =         $_POST['haircolor'];
$Occupation                =         $_POST['occupation'];
$Academic_Info             =         $_POST['ainfo'];
$Alias                     =         $_POST['alias'];
$Caste                     =         $_POST['caste'];
$Sect                      =         $_POST['sect'];
$Remarks                   =         $_POST['remarks'];

    $photo    =   $_POST['file'];   // giving error : undefined index/ getting nothing from the form.

我的ajax功能是:

<script>
var frm = $('.wrefresh');

frm.submit(function (ev)
{

    ev.preventDefault();

    var postdate = $(this).serialize();
    var path = $(this).attr("action");
    var mehtodtype = $(this).attr("method").toUpperCase();

    $(".loadingimg").show();

    $.ajax
    ({
        type: mehtodtype,
        url: path,
        data: postdate,
        success: function(data) 
        {
            // Get Form Data.
            $("#FIRtable").html(data);
            $("#Accusedtable").html(data);

            // Clear fields data.
            $('form :input[type=text], textarea').attr('value', '');

            // show hide icons when click on submit.
            $(".loadingimg").delay(1000).fadeOut();
            setTimeout(function()
            {
                $(".okicon").fadeIn('slow');
                $(".okicon").delay(2800).fadeOut();
            }, 1800);
        }
    });
});
</script>

我认为我的错误是因为我正在使用的ajax功能。除了$photo = $_POST['file'];这个//给出错误:undefined index之外,我得到了所有工作。帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

通过AJAX上传文件要比引用$_FILES数组复杂得多。你需要这样的东西来处理文件上传:

function upload_file() {
    var formData = new FormData( document.getElementById( "form-uploader" ));

    $.ajax( {
        url         : "Code Files/Accused.php",
        type        : "post",
        xhr         : function() {
            my_xhr = $.ajaxSettings.xhr();
            if( my_xhr.upload ) {
                my_xhr.upload.addEventListener( "progress", handler_progress, false );
            }

            return my_xhr;
        },

        beforeSend  : handler_before_send,
        success     : handler_complete,
        error       : handler_error,
        data        : formData,
        contentType : false,
        processData : false
    } );
}

此代码与您的代码之间的主要区别在于它使用了JavaScript FormData对象,这是使用AJAX上传文件所必需的。

然后在上传开始时调用各种支持函数,它的进度是什么,是否有任何错误,以及何时完成:

function handler_progress( e ) {
    if( e.lengthComputable ) {
        $( "progress" ).attr( {value:e.loaded, max:e.total} );
    }
}

function handler_before_send( e ) {
    var progress = $( "<progress></progress>" ).attr( "id", "progress-bar" );
    $( "#form-uploader" ).append( progress );
}

function handler_error( e ) {
    alert( "error" + e );
}

function handler_complete( e ) {
    // The browser has completed the upload, do any clean-up here
}

这主要是从我的一个项目中复制并粘贴,我在那里做你正在描述的事情(通过一些更改将它与你的代码联系起来),所以你会看到ID引用的一些元素在我的HTML中,您将不得不修改。但这基本上就是你要通过AJAX上传文件的目的。