php - 如何使用 PHP 和 HTML 将特定的表单数据插入到下一个网页的表单文本字段中?

标签 php html mysql forms

我想要实现的是将在我的“index.html”中输入的用户名插入到我下一个名为“report.html”的网页的 tfUser 文本字段中。我希望发生这种情况,因为我想提交用户的用户名以及“report.html”中的表单数据

我使用 1 个 MySQL 数据库作为我的数据源。

这是我的代码:

index.html

    <!doctype html>

<html class="">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Centro Escolar University - CMT System 2013</title>
<link href="boilerplate.css" rel="stylesheet" type="text/css">
<link href="index.css" rel="stylesheet" type="text/css">

<script src="respond.min.js"></script>
</head>
<body background="images/bg_new3.png">
<div data-role="page">
    <div data-role="header">
        <h1 align="center" class="futuraFont" style="color: #CC0066"><strong>SAMPLE</strong></h1>
    </div>
    <form id="form1" name="form1" method="POST" action="loginsub1.php">
    <div data-role="content">   

  <p align="center">
    <label for="userName"><strong>Username</strong><br>
    </label>
    <input type="text" name="userName" id="userName" />
  </p>
  <p align="center">
    <label for="userPass"><strong>Password</strong><br>
    </label>
    <input type="password" name="userPass" id="userPass" />
  </p>
  <p align="center">
    <input type="submit" name="button" id="button" value="LOGIN" />
  </p>
</form>

    </div>
    <div data-role="footer">
        <h4 align="center" style="color: #666666">FOOTER</h4>
  </div>
</body>
</html>

report.html

<!doctype html>

<html class="">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sample Form</title>
<link href="boilerplate.css" rel="stylesheet" type="text/css">
<link href="report.css" rel="stylesheet" type="text/css">

<script src="respond.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
body {
    background-repeat: no-repeat;
    background-attachment:fixed;
    background-position:center;
    background-size:1100px;
}
</style>

</head>
<body bgcolor="#D6D6D6" background="images/bg_new3.png">
<div data-role="page">
    <div data-role="header">
        <h1 align="center" class="futuraFont" style="color: #CC0066"><strong>SAMPLE</strong></h1>
  </div>
    <div data-role="content">
    <form id="form2" name="form2" method="POST" action="reportsub1.php">

    <label for="user">
            <div align="center"><strong>User</strong>: </div>
        </label>
          <div align="center">
            <input type="text" name="tfUser" id="tfUser" readonly  onfocus="this.blur();"Required>
            <br>
      </div>
        <label for="tfDate">
          <div align="center"><strong>Date</strong>:</div>
        </label>
          <div align="center">
            <input name="tfDate" id="nowDate" readonly  onfocus="this.blur();"Required/>
            <script type="text/javascript">

                //for getting system time
                function getCurrentTime() {
                    var currentTime = new Date()
                    var month = currentTime.getMonth() + 1
                    var day = currentTime.getDate()
                    var year = currentTime.getFullYear()
                    //return (month + "/" + day + "/" + year)

                    if (month<10){
                        var month1 = "0"+month
                        if (day<10){
                            var day1 = "0"+day
                            return (month1 + "/" + day1+ "/"+year)
                        }else{
                            return (month1 + "/" + day+ "/"+year)
                        }
                    }else{
                        if (day<10){
                                var day1 = "0"+day
                                return (month + "/" + day1+ "/"+year)
                        }else{  
                                return (month + "/" + day+ "/"+year)
                        }
                    }
                }
                document.getElementById("nowDate").value = getCurrentTime();
                //for getting system time

                </script>
            <br>
      </div>

      <label for="tfDay">
          <div align="center"><strong>Day</strong>:</div>
        </label>
          <div align="center">
            <input name="tfDay" id="nowDay" readonly  onfocus="this.blur();"Required/>
            <script type="text/javascript">

                //for getting system time
                function getCurrentDay() {
                    var d=new Date();
                    var weekday=new Array(7);
                    weekday[0]="Sunday";
                    weekday[1]="Monday";
                    weekday[2]="Tuesday";
                    weekday[3]="Wednesday";
                    weekday[4]="Thursday";
                    weekday[5]="Friday";
                    weekday[6]="Saturday";

                    var n = weekday[d.getDay()];

                    return (n)
                }
                document.getElementById("nowDay").value = getCurrentDay();


                </script>
            <br>
      </div>

        <label for="tfTime">
            <div align="center"><strong>Time</strong>:</div>
        </label>
        <div align="center">
            <input name="tfTime"  id="nowTime" readonly  onfocus="this.blur();"Required/>
          <script type="text/javascript">


                function getCurrentTime() {
                  var d = new Date();
                  function z(n){ return (n<10? '0':'') + n;}
                  return z(d.getHours()) + 
                         z(d.getMinutes());
                }
                document.getElementById("nowTime").value = getCurrentTime();

                </script>
          <br>
      </div>
        <label for="room">
            <div align="center"><strong>Room</strong>: </div>
        </label>
          <div align="center">
            <input type="text" name="tfRoom" id="room" Required>
            <br>
      </div>
          <label for="status">
            <div align="center"><strong>Status</strong></div>
          </label>
          <div align="center">
            <select name="status" id="status">
              <option value="-">--Select One--</option>
              <option value="1">Choice#1</option>
              <option value="0">Choice#2</option>
              <option value="-">Choice#3</option>
            </select>
            <br>
      </div>
          <label for="tfRemarks">
            <div align="center"><strong>Remarks</strong>:</div>
          </label>
          <div align="center">
            <input type="text" name="tfRemarks" id="tfRemarks">
            <br>
            <input type="submit" name="btSubmit" id="btSubmit" value="Submit Report">
          </div>
        </p>
      </form>
    </div>
    <div data-role="footer">
        <h4 align="center">&nbsp;</h4>
    </div>
</div>
</body>
</html>

我使用 php 代码将它们插入到我的数据库中。

提前感谢那些愿意帮助我的人

最佳答案

如果您在 index.html 中看到您的表单标签,如下所示

<form id="form1" name="form1" method="POST" action="loginsub1.php">

请参阅 action="loginsub1.php"。您正在将表单发布到 loginsub1.php。您可以在 $_POST 数组中获取数据。您可以在 session 中设置该用户名值,然后您可以在 Web 应用程序的任何位置获取 session 值。例如在 loginsub1.php 你可以使用

 session_start();
 $_SESSION['username'] =  $_POST['userName'];

report.html 中(将其更改为 report.php),您可以访问存储在上一页中的 session 值。

 <input type="text" value="<?php echo $_SESSION['username']; ?>" name="tfUser" id="tfUser" readonly  onfocus="this.blur();"Required>

关于php - 如何使用 PHP 和 HTML 将特定的表单数据插入到下一个网页的表单文本字段中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18590587/

相关文章:

java - Android (libGDX) 使用 PHP 服务器端脚本与 mySQL 进行 http 连接

javascript - 使用ajax从同一页面提交多个表单

php - 将 session 安全地传递到另一个浏览器的最佳方法

html - float 元素在不同的屏幕分辨率下消失

html - 最小化标题图像和博客文章之间的空间

php - 如何使用mysql统计每天的一条记录

PHPExcel,自动调整行高

css - 如何垂直对齐列内容并设置适合的列宽?

php - 带有 COUNT 别名的 MySQL UNION

javascript - 我应该通过 PHP 还是 JavaScript 进行哈希处理?