如何将页面内容传递到另一个页面,然后将其保存到Mysql中
How to pass a page content to another page and then save it into Mysql?
我有一个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
相关文章:
- 在变量中保存值的最佳方法是在应用程序关闭后使用,然后使用apachecordova在android中重新启动
- Angular js,在选择元素上,我想 POST 数据以将其保存在数据库中,然后我想使用 PUT 更新它而无需重新加载
- 将图像输出到浏览器,然后再将其保存到文件夹
- firefox扩展可以修改HTML文档的DOM,然后保存为HTML
- 在表中创建带有输入的新行,然后单击保存该输入的值
- Metor:将html保存为字符串,然后将其转换为DOM节点
- 如何将文本输入保存为变量,然后提交到服务器(ajax 和 javascript)
- 如何将数据保存在一个 HTML 页面中,然后在 Phonegap 中的另一个 HTML 页面上显示
- 从“选项”弹出窗口中选择一个值,然后将其保存到 Chrome.storage,以便在后台脚本中用作值
- 记录链接单击,然后保存到本地存储
- 创建一个带有背景图像的画布,动态更新文本,然后保存画布
- 将规则添加到CSS类,然后保存样式表
- 如何将表单中的 Json 发布到控制器,然后保存到数据库
- XML格式的HTML,然后保存为文件
- 索引目录,然后保存到变量?node . js
- 从JSON取回,编辑模型数据,然后保存回JSON
- JS调整图像大小,然后保存调整大小的图像
- JQGrid一次更新多行,然后保存
- Javascript Croppie.从客户端url裁剪图像,然后保存
- 我怎么能看到一个预览上传的图像(don!),然后保存它的地址