簡體   English   中英

照片未存儲到數據庫中。 php [未定義的索引]

[英]photo not storing onto the database. php [undefined Index]

我有這樣的形式:

<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'];之外,我正在使所有工作正常$photo = $_POST['file']; 這//給出錯誤: undefined index 幫助將不勝感激。

通過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上傳文件的目的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM