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

时间:2015-07-30 05:42:32

标签: javascript php jquery html mysql

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

问题:

我希望在其他页面上重定向时,将我的按钮内容(用户使用按钮上传的文件)传递到另一页面。然后将其保存到Mysql Db。

这是我的按钮代码:

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>

带按钮的内容。然后我想将它存储在数据库中。

如何传递网页内容?

2 个答案:

答案 0 :(得分:0)

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

然后在m2.php中,您可以使用$_SESSION["filename"]

检索相同内容

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

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

答案 1 :(得分:0)

这样的东西?

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