doulan8152 2014-10-05 22:23
浏览 80

html表单动作和onclick

Here is a php file named upload_file.php :

<?php
  if ($_FILES["file"]["error"] > 0) {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
  } else {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
    if (file_exists("upload/" . $_FILES["file"]["name"])) {
      echo $_FILES["file"]["name"] . " already exists. ";
    } else {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
    }
  }
?>

When I call the above file with the html form:

<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

Everything works great and I manage to upload the selected file in the /upload local server folder.

My question: What is the equivalent of the above form if I want to use onclick? I.e. in the following code

<a href="#x" class="overlay" id="browse_file"></a>
        <div class="popup">
            <h2>Browse for your file</h2>
            <input type="file" name="file" id="file"><br>
            <input type="button" value="file" onclick= "location.href = 'upload_file.php'">
        </div>

that defines a popup window (that is controled by anothel modal dialog - thats why I do not want to use the form), what should be completed in order to pass the filename chosen to $_FILES when calling upload_file.php with onclick?

Thank you friends.

  • 写回答

1条回答 默认 最新

  • doufeinai6081 2014-10-05 22:40
    关注

    You should read about AJAX and javascript if you want to use onclick function with forms. There is very simple implementation of the ajax request in the jquery. Also you can easily get chosen file from the input by using jquery.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法