Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过ajax和jquery从另一个php文件上传图像后显示图像_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 通过ajax和jquery从另一个php文件上传图像后显示图像

Javascript 通过ajax和jquery从另一个php文件上传图像后显示图像,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,加载页面时,会显示src中指定的图像。当用户单击表单上载图像时,一切正常,但页面上的图像不会更改。 这是因为当用户点击表单上传图像时,他被引导到php文件2,但从那里没有请求更改php文件1中的图像。如何实现这一点(使用ajax和jquery)?试试这段代码。这是核心 <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.

加载页面时,会显示src中指定的图像。当用户单击表单上载图像时,一切正常,但页面上的图像不会更改。
这是因为当用户点击表单上传图像时,他被引导到php文件2,但从那里没有请求更改php文件1中的图像。如何实现这一点(使用ajax和jquery)?

试试这段代码。这是核心

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
            $('#imgInp').on('change', function() {
            readPath(this);
            });
            });

            function readPath(input) {
            if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
          $('#blah').attr('src', e.target.result);
            }
          reader.readAsDataURL(input.files[0]);
                }
            }
        </script>
    </head>
    <body>
<form id="form1">
    <input type='file' id="imgInp" />
    <img id="blah" src="#" width="500px" height="200px" alt="your image" />
</form>

    </body> 
</html>

$(文档).ready(函数(){
$('imgInp')。on('change',function(){
读取路径(本);
});
});
函数读取路径(输入){
if(input.files&&input.files[0]){
var reader=new FileReader();
reader.onload=函数(e){
$('#blah').attr('src',e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}

试试这段代码。这是核心

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
            $('#imgInp').on('change', function() {
            readPath(this);
            });
            });

            function readPath(input) {
            if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
          $('#blah').attr('src', e.target.result);
            }
          reader.readAsDataURL(input.files[0]);
                }
            }
        </script>
    </head>
    <body>
<form id="form1">
    <input type='file' id="imgInp" />
    <img id="blah" src="#" width="500px" height="200px" alt="your image" />
</form>

    </body> 
</html>

$(文档).ready(函数(){
$('imgInp')。on('change',function(){
读取路径(本);
});
});
函数读取路径(输入){
if(input.files&&input.files[0]){
var reader=new FileReader();
reader.onload=函数(e){
$('#blah').attr('src',e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}

试试这段代码。这是核心

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
            $('#imgInp').on('change', function() {
            readPath(this);
            });
            });

            function readPath(input) {
            if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
          $('#blah').attr('src', e.target.result);
            }
          reader.readAsDataURL(input.files[0]);
                }
            }
        </script>
    </head>
    <body>
<form id="form1">
    <input type='file' id="imgInp" />
    <img id="blah" src="#" width="500px" height="200px" alt="your image" />
</form>

    </body> 
</html>

$(文档).ready(函数(){
$('imgInp')。on('change',function(){
读取路径(本);
});
});
函数读取路径(输入){
if(input.files&&input.files[0]){
var reader=new FileReader();
reader.onload=函数(e){
$('#blah').attr('src',e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}

试试这段代码。这是核心

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
            $('#imgInp').on('change', function() {
            readPath(this);
            });
            });

            function readPath(input) {
            if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
          $('#blah').attr('src', e.target.result);
            }
          reader.readAsDataURL(input.files[0]);
                }
            }
        </script>
    </head>
    <body>
<form id="form1">
    <input type='file' id="imgInp" />
    <img id="blah" src="#" width="500px" height="200px" alt="your image" />
</form>

    </body> 
</html>

$(文档).ready(函数(){
$('imgInp')。on('change',function(){
读取路径(本);
});
});
函数读取路径(输入){
if(input.files&&input.files[0]){
var reader=new FileReader();
reader.onload=函数(e){
$('#blah').attr('src',e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}

这里是另一个代码片段。在服务器部分检查您的条件,不要忘记在服务器端和映像src中正确提供位置

HTML部分

<!doctype html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<style>
form { display: block; margin: 20px auto; background: #eee; border-radius: 10px; padding: 15px }
#progress { position:relative; width:400px; border: 1px solid #ddd; padding: 1px; border-radius: 3px; }
#bar { background-color: #B4F5B4; width:0%; height:20px; border-radius: 3px; }
#percent { position:absolute; display:inline-block; top:3px; left:48%; }
</style>
</head>
<body>
<h1>Ajax File Upload Demo</h1>
 <img id="blah" src="#" width="500px" height="200px" alt="your image" />
<form id="myForm" action="upload.php" method="post" enctype="multipart/form-data">
     <input type="file" size="60" name="myfile">
     <input type="submit" value="Ajax File Upload">
 </form>

 <div id="progress">
        <div id="bar"></div>
        <div id="percent">0%</div >
</div>
<br/>

<div id="message"></div>

<script>
$(document).ready(function()
{

    var options = { 
    beforeSend: function() 
    {
        $("#progress").show();
        //clear everything
        $("#bar").width('0%');
        $("#message").html("");
        $("#percent").html("0%");
    },
    uploadProgress: function(event, position, total, percentComplete) 
    {
        $("#bar").width(percentComplete+'%');
        $("#percent").html(percentComplete+'%');

    },
    success: function() 
    {
        $("#bar").width('100%');
        $("#percent").html('100%');

    },
    complete: function(response) 
    {
        $("#blah").attr("src",response.responseText);
    },
    error: function()
    {
        $("#message").html("<font color='red'> ERROR: unable to upload files</font>");

    }

}; 

     $("#myForm").ajaxForm(options);

});

</script>
</body>

</html>

窗体{显示:块;边距:20px自动;背景:#eee;边框半径:10px;填充:15px}
#进度{位置:相对;宽度:400px;边框:1px实心#ddd;填充:1px;边框半径:3px;}
#条{背景色:#B4F5B4;宽度:0%;高度:20px;边框半径:3px;}
#百分比{位置:绝对;显示:内联块;顶部:3px;左侧:48%;}
Ajax文件上传演示
0%

$(文档).ready(函数() { 变量选项={ beforeSend:function() { $(“#进度”).show(); //清除一切 $(“#bar”).width('0%); $(“#消息”).html(“”); $(“#百分比”).html(“0%”); }, 上载进度:功能(事件、位置、总数、完成百分比) { $(“#条”).width(完成百分比+'%'); $(“#百分比”).html(完成百分比+'%'); }, 成功:函数() { $(“#bar”).宽度('100%); $(“#percent”).html('100%); }, 完成:功能(响应) { $(“blah”).attr(“src”,response.responseText); }, 错误:函数() { $(“#message”).html(“错误:无法上载文件”); } }; $(“#myForm”).ajaxForm(选项); });
Upload.php

<?php
//upload.php
$output_dir = "C:/wamp/www/";

if(isset($_FILES["myfile"]))
{
    //Filter the file types , if you want.
    if ($_FILES["myfile"]["error"] > 0)
    {
      echo "Error: " . $_FILES["file"]["error"] . "<br>";
    }
    else
    {
        //move the uploaded file to uploads folder;
        move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $_FILES["myfile"]["name"]);

     echo $_FILES["myfile"]["name"];
    }

}
?>

这里是另一个代码片段。在服务器部分检查您的条件,不要忘记在服务器端和映像src中正确提供位置

HTML部分

<!doctype html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<style>
form { display: block; margin: 20px auto; background: #eee; border-radius: 10px; padding: 15px }
#progress { position:relative; width:400px; border: 1px solid #ddd; padding: 1px; border-radius: 3px; }
#bar { background-color: #B4F5B4; width:0%; height:20px; border-radius: 3px; }
#percent { position:absolute; display:inline-block; top:3px; left:48%; }
</style>
</head>
<body>
<h1>Ajax File Upload Demo</h1>
 <img id="blah" src="#" width="500px" height="200px" alt="your image" />
<form id="myForm" action="upload.php" method="post" enctype="multipart/form-data">
     <input type="file" size="60" name="myfile">
     <input type="submit" value="Ajax File Upload">
 </form>

 <div id="progress">
        <div id="bar"></div>
        <div id="percent">0%</div >
</div>
<br/>

<div id="message"></div>

<script>
$(document).ready(function()
{

    var options = { 
    beforeSend: function() 
    {
        $("#progress").show();
        //clear everything
        $("#bar").width('0%');
        $("#message").html("");
        $("#percent").html("0%");
    },
    uploadProgress: function(event, position, total, percentComplete) 
    {
        $("#bar").width(percentComplete+'%');
        $("#percent").html(percentComplete+'%');

    },
    success: function() 
    {
        $("#bar").width('100%');
        $("#percent").html('100%');

    },
    complete: function(response) 
    {
        $("#blah").attr("src",response.responseText);
    },
    error: function()
    {
        $("#message").html("<font color='red'> ERROR: unable to upload files</font>");

    }

}; 

     $("#myForm").ajaxForm(options);

});

</script>
</body>

</html>

窗体{显示:块;边距:20px自动;背景:#eee;边框半径:10px;填充:15px}
#进度{位置:相对;宽度:400px;边框:1px实心#ddd;填充:1px;边框半径:3px;}
#条{背景色:#B4F5B4;宽度:0%;高度:20px;边框半径:3px;}
#百分比{位置:绝对;显示:内联块;顶部:3px;左侧:48%;}
Ajax文件上传演示
0%

$(文档).ready(函数() { 变量选项={ beforeSend:function() { $(“#进度”).show(); //清除一切 $(“#bar”).width('0%); $(“#消息”).html(“”); $(“#百分比”).html(“0%”); }, 上载进度:功能(事件、位置、总数、完成百分比) { $(“#条”).width(完成百分比+'%'); $(“#百分比”).html(完成百分比+'%'); }, 成功:函数() { $(“#bar”).宽度('100%); $(“#percent”).html('100%); }, 完成:功能(响应) { $(“blah”).attr(“src”,response.responseText); }, 错误:函数() { $(“#message”).html(“错误:无法上载文件”); } }; $(“#myForm”).ajaxForm(选项); });
Upload.php

<?php
//upload.php
$output_dir = "C:/wamp/www/";

if(isset($_FILES["myfile"]))
{
    //Filter the file types , if you want.
    if ($_FILES["myfile"]["error"] > 0)
    {
      echo "Error: " . $_FILES["file"]["error"] . "<br>";
    }
    else
    {
        //move the uploaded file to uploads folder;
        move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $_FILES["myfile"]["name"]);

     echo $_FILES["myfile"]["name"];
    }

}
?>

这里是另一个代码片段。在服务器部分检查您的条件,不要忘记在服务器端和映像src中正确提供位置

HTML部分

<!doctype html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<style>
form { display: block; margin: 20px auto; background: #eee; border-radius: 10px; padding: 15px }
#progress { position:relative; width:400px; border: 1px solid #ddd; padding: 1px; border-radius: 3px; }
#bar { background-color: #B4F5B4; width:0%; height:20px; border-radius: 3px; }
#percent { position:absolute; display:inline-block; top:3px; left:48%; }
</style>
</head>
<body>
<h1>Ajax File Upload Demo</h1>
 <img id="blah" src="#" width="500px" height="200px" alt="your image" />
<form id="myForm" action="upload.php" method="post" enctype="multipart/form-data">
     <input type="file" size="60" name="myfile">
     <input type="submit" value="Ajax File Upload">
 </form>

 <div id="progress">
        <div id="bar"></div>
        <div id="percent">0%</div >
</div>
<br/>

<div id="message"></div>

<script>
$(document).ready(function()
{

    var options = { 
    beforeSend: function() 
    {
        $("#progress").show();
        //clear everything
        $("#bar").width('0%');
        $("#message").html("");
        $("#percent").html("0%");
    },
    uploadProgress: function(event, position, total, percentComplete) 
    {
        $("#bar").width(percentComplete+'%');
        $("#percent").html(percentComplete+'%');

    },
    success: function() 
    {
        $("#bar").width('100%');
        $("#percent").html('100%');

    },
    complete: function(response) 
    {
        $("#blah").attr("src",response.responseText);
    },
    error: function()
    {
        $("#message").html("<font color='red'> ERROR: unable to upload files</font>");

    }

}; 

     $("#myForm").ajaxForm(options);

});

</script>
</body>

</html>

窗体{显示:块;边距:20px自动;背景:#eee;边框半径:10p