选择文件后提交PHP上载

时间:2016-04-19 20:43:16

标签: javascript php jquery html

我有一个使用PHP的图像上传脚本,带有一个简单的多文件选择,然后是一个上传函数,如下所示:

GLfloat color1 = {0.633, 0.237, 0.170}; \\ changed to blue

void initLight()
{

    GLfloat red[] = {1.0,0.0,0.0,1.0};
    GLfloat white[] = {1.0,1.0,1.0,1.0};
    GLfloat blueGreen[]     = {0.0,0.4,1.0,1.0};

    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, white); 
    glLightfv(GL_LIGHT0,GL_AMBIENT,white); 
    glLightfv(GL_LIGHT0,GL_DIFFUSE,blueGreen);
    glMaterialf(GL_FRONT,GL_SHININESS,127.0); 
    glEnable(GL_LIGHT0);

}

HTML:

mysql_connect("localhost", "root", "") or die("error");
mysql_select_db("repo") or die("error");

$imgerror = '';
if(isset($_POST['log'])){
    foreach($_FILES['files']['tmp_name'] as $key => $name_tmp){
        $name = $_FILES['files']['name'][$key];
        $tmpnm = $_FILES['files']['tmp_name'][$key];
        $type = $_FILES['files']['type'][$key];
        $size = $_FILES['files']['size'][$key];
        $dir = "content/images/".$name;
        $move = move_uploaded_file($tmpnm,$dir);
        if($move){
            $hsl = mysql_query("insert into files values('','$client','$name','$type','$size',now())");

            if ($hsl){
                $imgerror = "IMAGE(S) UPLOADED SUCCESSFULLY";
            } else {
                $imgerror = "CANNOT CONNECT TO DATABASE";
            }
        } else {
            $imgerror = "NO IMAGES SELECTED";
        }
    }
}

我确实从另一个问题here中找到了一些使用jQuery的信息,但我不知道如何将其实现到我的代码中,或者可能有人可以提出替代方案。我要做的就是选择文件,并在不按下提交按钮的情况下自动提交。

任何帮助将不胜感激, 感谢

1 个答案:

答案 0 :(得分:1)

onchange事件适用于输入类型文件。下一个代码自动提交“一旦选择了文件”(在Mozilla Firefox中测试):

<html>
  <head>
    <script type="text/javascript">

function on_change ()
{ alert( "File(s) chosen!" +
         "\n\n" +
         "Click to submit files to upload." );
  document.getElementById( "frm" ).submit();
}
    </script>
  </head>
  <body>
    <form id="frm" action="upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="files[]" multiple onchange="on_change()">
        <input type="submit" name="log" value="Upload">
    </form>
  </body>
</html>

当然,您必须删除JavaScript“警告”窗口,它会显示“onchange”事件有效。