Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

AJAX取得了PHP的返回值,想要input上传区显示图片,却无法显示

将数据存入数据库PHP:
<?PHP
    header("Content-Type: text/html; charset=utf-8");
    include('conn.php');//链接数据库
    //1、设置变量来接收表单提交过来的值
    $article_name = $_POST['art_name'];
    $checkbox = $_POST['checkbox'];
    $content = $_POST['content'];
    $input_name = $_POST['input_name'];
    $input_number = $_POST['input_number'];
    $input_ewm = $_POST['input_ewm'];
    $input_title = $_POST['input_title'];
    $input_describe = $_POST['input_describe'];
    $input_image = $_POST['input_image'];
    //2、将数组转为字符串
    $check_box=implode(',',$checkbox);
    //3、插入到数据库
    $sql="insert into article(id,article_name,check_box,radio_box,content,input_name,input_number,input_ewm,input_title,input_describe,input_image) values(null,'$article_name','".$check_box."','$_POST[radio_box]','$content','$input_name','$input_number','$input_ewm','$input_title','$input_describe','$input_image')";
    $result = mysqli_query($conn,$sql);
    echo $sql;
?>
PHP:
<?PHP
    header("Content-Type: text/html; charset=utf-8");

    include('conn.php');//链接数据库

    $sql = "select * from article";
    $res = mysqli_query($conn,$sql);

    $arr = [];

    while ($row = mysqli_fetch_array($res)) {
        $arr[] = $row['article_name'];
        $arr[] = $row['check_box'];
        $arr[] = $row['radio_box'];
        $arr[] = $row['content'];
        $arr[] = $row['input_name'];
        $arr[] = $row['input_number'];
        $arr[] = $row['input_ewm'];
        $arr[] = $row['input_title'];
        $arr[] = $row['input_describe'];
        $arr[] = $row['input_image'];
    }

    echo json_encode($arr);

?>
HTML:
<div class="div_imgall">
    <input type="file" class="input_flie" name="input_ewm" rel="file" id="input_ewm"/>
    <div class="jiahao">
        <div class="div_shuline"></div>
        <div class="div_hengline"></div>
    </div>
    <img src="" alt="" id="preview" style="position: absolute;width: 120px;height: 120px;padding: 8px 12px 12px 9px;z-index: 6;"/>
</div>
<script>
//编辑获取数据库数据
$(document).ready(function(){
    $.ajax({
        url : "./php/edit.php",//后台请求的数据,用的是PHP
        type : "post",//请求方式
        async : false,//是否异步请求
        success : function(msg) {?//如果请求成功,返回数据。
            var data = JSON.parse(msg);
            $("#art_name").val(data[0]);
            $("#preview").attr("src",data[6]);
            $("#img_ewm").attr("src",data[6]);
            console.log(data[6]);
        },
    })
})
</script>

编辑页想要获取数据库中的名称,AJAX已经获取到了返回值:

xigua.jpg

image

Ajax有获取到图片返回值,想要在input上传区域输出图片#prview但是是空白的,加"src"属性,会提示图片404。

Failed to load resource: the server responded with a status of 404 (Not Found)

怎么样能让input上传框中的#prview显示获取到图片?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

路径不对呗,浏览器能显示嘛


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...