未定义的索引:头像

时间:2017-10-15 18:14:18

标签: javascript php

我有这个代码上传头像..它工作正常,但我得到未定义的索引头像,如果我提交表单与空头像。第一个代码是php代码,然后是Html代码,最后一个代码是fileinput代码。谢谢你帮助好朋友。

    if(isset($_POST['submit'])){

        $adminID = $_SESSION['adminID'];

        if($_POST['avatar'] == ""){
            $error[] = "Please Select a Picture!";
            }

        $type = explode('.', $_FILES['avatar']['name']);
        $type = $type[count($type)-1];      
        $url = 'assets/images/users/'.$_SESSION['username'].'.'.$type;
        if(in_array($type, array('gif', 'jpg', 'jpeg', 'png', 'JPG', 'GIF', 'JPEG', 'PNG'))) {
            if(is_uploaded_file($_FILES['avatar']['tmp_name'])) {           
                if(move_uploaded_file($_FILES['avatar']['tmp_name'], $url)) {

                    try {   //insert into database with a prepared statement
                            $stmt = $db->prepare("UPDATE admin SET avatar = '$url' WHERE adminID =  $adminID");
                            $stmt->execute(array(
                                $adminID
                            ));

                            echo '<META HTTP-EQUIV="Refresh" Content="0; URL='.$location.'">';
                            exit;
                        } 
                    //else catch the exception and show the error.
                    catch(PDOException $e) {
                            $error[] = $e->getMessage();
                        }                       
                }       
            } 
        }   
    }
?>

以下是文件输入的HTML CODE,用户将使用该代码选择头像

    <div class="col-sm-4 text-center">
        <?php
            if(isset($error)){
                foreach($error as $error){
                    echo '<p class="btn btn-warning">'.$error.'</p>';

                }                                           
            }
        ?> 
            <input type="file" id="avatar" name="avatar" class="file" data-preview-file-type="any" />
        </div>
        <button type="submit" id="submit" name="submit" class="btn btn-success pull-right">Save <span class="fa fa-floppy-o fa-right"></span></button>

这是我的fileinput插件初始化代码

<script>
    var btnCust = '<button type="button" class="btn btn-secondary" title="Add picture tags" ' + 
        'onclick="alert(\'Call your custom code here.\')">' +
        '<i class="glyphicon glyphicon-tag"></i>' +
        '</button>'; 
    $("#avatar").fileinput({
        overwriteInitial: true,
        maxFileSize: 1500,
        showClose: false,
        showCaption: false,
        showBrowse: false,
        browseOnZoneClick: true,
        removeLabel: '',
        removeIcon: '<i class="fa fa-trash-o"></i>',
        removeTitle: 'Cancel or Delete Selection',
        elErrorContainer: '#kv-avatar-errors-2',
        msgErrorClass: 'alert alert-block alert-danger',
        defaultPreviewContent: '<img src="" alt=""><h6 class="text-muted">Click to select</h6>',
        layoutTemplates: {main2: '{preview} ' +  btnCust + ' {remove} {browse}'},
        allowedFileExtensions: ["jpg", "png", "gif"]
    });
</script>

1 个答案:

答案 0 :(得分:1)

您已将ImageButton ImageButton3; TextView TextView26; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageButton3 = (ImageButton) findViewById(R.id.myImgBtnId); TextView26 = (TextView) findViewById(R.id.myTvId); ImageButton3.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { TextView26.text=""; } }); 字段作为avatar数组的成员引用,然后作为$_POST数组的成员引用。

只需使用以下内容更改$_FILES条件即可删除错误并实现您要执行的逻辑。

if