【问题标题】:submit form automatically after select image from browse window while uploading image上传图片时从浏览窗口选择图片后自动提交表单
【发布时间】:2013-10-30 19:01:05
【问题描述】:

我想上传图片,也想将该图片存储在数据库中。但是当我点击图片打开浏览文件对话框时,表单已经提交。但是我想在选择图片后提交表单。

这是我的代码

<form>

<input type="file" id="my_file" style="display:none;" />
<input type="image" src="albert-einstein-bike.jpg" width="90px" height="200" />
</form>

 <script>

$("input[type='image']").click(function() {
$("input[id='my_file']").click();
});
</script>

【问题讨论】:

    标签: jquery image upload


    【解决方案1】:

    希望这样的事情可能对你有用... :)

    html

    <div id='preview'></div>
        <form id="imageform" method="post" enctype="multipart/form-data" action='ajaximage.php'>
        <input type="file" name="photoimg" id="photoimg" />
         </form>
    

    脚本文件

    $('#photoimg').on('change', function() 
     {
          $("#imageform").ajaxForm({target: '#preview', //Shows the response image in the div named preview 
             success:function(){
    
             }, 
             error:function(){
    
              } 
           }).submit();
    });
    

    ajaximage.php

    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
    {
    $name = $_FILES['photoimg']['name'];
    $size = $_FILES['photoimg']['size'];
    $tmp = $_FILES['photoimg']['tmp_name'];
    $path = "uploads/";
    move_uploaded_file($tmp, $path.$name) //Stores the image in the uploads folder
    }
    

    【讨论】:

    • 兄弟我无法提交表单。请检查一下
    • Uncaught TypeError: Object [object Object] has no method 'live'
    • 使用on 而不是live mate.. :)
    • Uncaught TypeError: Object [object Object] has no method 'ajaxForm'
    • 你可能没有正确添加脚本。检查你是否添加了 jquery form.js &lt;script src="http://malsup.github.com/jquery.form.js"&gt;&lt;/script&gt;
    猜你喜欢
    • 1970-01-01
    • 2021-11-09
    • 2018-07-13
    • 2013-06-22
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    相关资源
    最近更新 更多