如何将页面内容传递到另一个页面,然后将其保存到Mysql中

How to pass a page content to another page and then save it into Mysql?

本文关键字:然后 保存 Mysql 另一个      更新时间:2023-09-26

我有一个JavaScript按钮代码。此按钮用作文件上传按钮。当用户点击此按钮时,将打开一个文件浏览器窗口,用户选择要上传的文件。选择文件后,页面自动重定向到另一个页面。

问题:

我想通过我的按钮的内容(由用户使用按钮上传的文件)到另一个页面,而在其他页面上重定向。然后保存到Mysql数据库。

这是我的按钮代码:

a1.php

<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>

mb.php中,我想替换下面的代码

<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>

带有按钮的内容。然后我想把它存储在db.

如何传递页面内容?

像这样?

<?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();
if(isset($_POST['submit'])) {
    //Here you can put your code when clicking the button
}

?>
<!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());
});   
   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>
 <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">  
      <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' name="submit" value='Submit' hidden="" /> 
        </form> 
   </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>

我注意到你正在php中开始一个会话。您可以使用会话变量将变量从一个页面转移到另一个页面,只要它们位于同一会话中。例如:$_SESSION["filename"]=$_POST["filename"];在a1.php存储您的文件名。(你可以通过$_POST为你的按钮获取值)

然后在m2.php中你可以通过使用$_SESSION["filename"]

来检索相同的

确保在需要此值的页面中有session_start()

这里是PHP会话的参考。http://php.net/manual/en/reserved.variables.session.php