【问题标题】:How to pass the content of one file upload button to another page?如何将一个文件上传按钮的内容传递到另一个页面?
【发布时间】:2015-10-20 04:56:31
【问题描述】:

我是 PHP 新手。我有一个从系统上传文件的按钮。我使用 javascript,当用户上传文件时,它会自动重定向到 Web 表单。在网络表单上,我使用三个字段。第一个是日期,第二个是标题,第三个是文件上传按钮。现在我想将我的文件内容传递给这个按钮。有可能吗?

我上传文件的按钮

上传文件后我的表单

我想在数据库中存储文件、日期和标题。为了节省用户的时间,我想传递文件内容。

我该怎么做?

请帮帮我

按钮代码

<html>
    <head> 
        <script>
            function setup() {
                document.getElementById('buttonid').addEventListener('click', openDialog);
                function openDialog() {
                    document.getElementById('fileid').click();
                }
                document.getElementById('fileid').addEventListener('change', submitForm);
                function submitForm() {
                    document.getElementById('formid').submit();
                }
            }
        </script> 
    </head>
    <body onload="setup()">
        <form id='formid' action="mb.php" method="POST" enctype="multipart/form-data"> 
            <input id='fileid' type='file' name='filename' hidden/>
            <input id='buttonid' type='button' value='Upload MB' /> 
            <input type='submit' value='Submit' hidden="" /> 
        </form> 
    </body> 
</html>

我的表单代码

mb.php

<?php

session_start();

//$user_id = $_SESSION['uid'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pacra-daily";

$conn = new mysqli($servername, $username, $password, $dbname);
//$id2 = $_GET['id'];
$sql="SELECT pacra_teams.title as 'teamTitle', og_users.display_name, og_users.id
FROM og_users
LEFT JOIN pacra_teams
ON pacra_teams.id = og_users.team_id
Where og_users.id = 20 ";
$result = $conn->query($sql);
$row = $result->fetch_object();

?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Upload Morning Briefing</title>
  <link rel="stylesheet" type="text/css" href="style.css">
  <link rel="stylesheet" href="js/jquery-ui.css">
  <script src="js/jquery-1.10.2.js"></script>
  <script src="js/jquery-ui.js"></script>

    <script>
$(document).ready(function() {
    $("#mydate").datepicker({
    dateFormat: "dd-M-y",
   onSelect: function(dateText, inst) {
            $("#dt_title input[type='text']").val($("#dt_title input[type='text']").attr('data-title')+dateText);
   }
    }).datepicker("setDate", new Date());
});   
</script>

</head>
<body>

 <form action="up_mb.php" method="POST" enctype="multipart/form-data">
<div style="margin:auto; width:auto" align="center">
   <table width="547" class="tblbdr" >
    <tr>
        <td height="23"  colspan="6" class="head"><p>  Morning Briefing </p></td>
</tr>
 <tr> <td height="10"></td></tr>
<tr><td class="celltext"><b>Date:</b> </td> <td><input name="mydate" type="text" id="mydate" style="width:300px" readonly> </td></tr>
<tr>

<tr><td class="celltext"><b>Title: </b><br> </td> 

<td class="celltext" style="width:200px" >  <span id="dt_title"> <input name="title" type="text" value=" MB | 
<?php echo $row->teamTitle;?> | <?php echo $row->display_name; ?> | <?php echo date("d-M-y");?>" 
data-title="MB | <?php echo $row->teamTitle;?> | <?php echo $row->display_name;?> | " style="width:300px"/ readonly> </span> </td> </tr>
</tr>
<td class="celltext"><b>Upload File:</b></td>
    <td colspan="4" bordercolorlight="#006666">  
    <input type="file" name="myfile" id="myfile" width="100%" size=80/>

   <!-- <input type="file" name="files[]"  multiple style="width:300px"/> -->
   </td></tr>
    <td><input type="submit" value="Save"/> </td> <td> </td>
    <td width="151">

   </td>
    <tr>
    <td height="12">
    </td>
    <td width="290">

   </td> </tr>
   </table>
   </div>
</form>

</body>
</html>

【问题讨论】:

  • 你想如何存储文件?什么样的数据库?你是如何访问数据库的?我们需要更多信息。
  • @Emz 我使用 PHP 和 Mysql 数据库。我希望当用户上传文件时它通过一些变量在我的表单上移动。
  • @Emz 现在请帮助我

标签: javascript php jquery file-upload


【解决方案1】:

出于安全原因,不能设置上传字段的文件

但是您可以使用第一个按钮将文件发送到服务器,然后使用第二个表单添加有关此上传的信息,这取决于您要如何处理这些数据

【讨论】:

  • 如何将文件发送到服务器?在我的代码中,当用户上传文件时,它会在表单上重定向。你能帮帮我吗?
  • 在第一个表单上,你在 $_FILES 的帮助下保存文件,然后重定向到第二个表单
  • 你能给我一些代码作为提示吗
  • 看看这个问题,它谈到了上传和重定向:stackoverflow.com/questions/12691842/html-php-file-upload
猜你喜欢
  • 1970-01-01
  • 2014-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多