提交表格并在不同的页面上阅读

时间:2016-12-07 06:57:18

标签: javascript php jquery html forms

我的网站上有一个表单,需要将提交的数据,提交日期和时间保存在html文件中,以便在不同页面上显示数据。

<form>
  <label>Name</label>      <input type="text" />
  <label>Email</label>      <input type="email" />
  <input type="submit" value="Submit">
</form>

表单看起来像这样。按下提交后,数据应写在现有内容上方的submissions.html文件中,以显示最新提交的内容。

*** 02 ***
Submited: 2016/12/06 04:32
Tony DiNozzo 
TonyDiNozzo@gmail.com

*** 01 ***
Submited: 2016/12/05 16:08
Timothy McGee
TimothyMcGee@gmail.com

在php或javascript中实现这一目标最优雅的方法是什么?

3 个答案:

答案 0 :(得分:1)

我认为使用php ...

<form method="post" action="submissions.php">
<label>Name</label><input type="text" name="name"/>
<label>Email</label><input type="email" name="email"/>
<input type="submit" value="Submit" name="submit">
</form>
在submissions.php中

date_default_timezone_set('Asia/Jakarta');
$creadate = date("Y-m-d H:i:s");
if(isset($_POST['submit']{
    $name = $_POST['name'];
    $email = $_POST['email'];

   echo "Submitted ". $creadate;
   echo $name;
   echo $email;
}

CMIIW,我是新手:3

答案 1 :(得分:1)

非常简单。你必须在下面的代码中创建一个名为 file.php 的文件:

<?php

session_start();

if (isset($_GET["name"]) && isset($_GET["email"]) && !empty($_GET["name"]) && !empty($_GET["email"])) {

    if(isset($_SESSION['view']))
    {
        $_SESSION['view']=$_SESSION['view']+1;
    }
    else
    {
        $_SESSION['view']=1;
    }

    $myfile = fopen("submissions.html", "a") or die("Unable to open file!");

    $line = "*** ". $_SESSION["view"] . " ***";
    $date = "Submitted: " . date("Y/m/d H:i", time());
    $name = $_GET["name"];
    $email = $_GET["email"];
    $line2 = "";

    fwrite($myfile, "\n". $line);
    fwrite($myfile, "\n". $date);
    fwrite($myfile, "\n". $name);
    fwrite($myfile, "\n". $email);
    fwrite($myfile, "\n". $line2);

    fclose($myfile);
} else {
    header('Location: index.html');
}

?>

index.html 这个内容:

<html>
<head>
    <title>Form</title>
</head>
<body>
    <form method="get" action="file.php">
        <label>Name</label>
        <input title="name" required name="name" type="text" />
        <label>Email</label>
        <input title="email" required name="email" type="email" />
        <input type="submit" value="Submit">
    </form>
</body>
</html>


解释:

此代码将计入当前会话。它是*** (int)+1 ***

所必需的
if(isset($_SESSION['view']))
{
    $_SESSION['view']=$_SESSION['view']+1;
}
else
{
    $_SESSION['view']=1;
}

在这里打开文件。 a代表append

$myfile = fopen("submissions.html", "a") or die("Unable to open file!");

此代码将格式化您的输出:

$line = "*** ". $_SESSION["view"] . " ***";
$date = "Submitted: " . date("Y/m/d H:i", time());
$name = $_GET["name"];
$email = $_GET["email"];
$line2 = "";

并且看起来像:
image

然后我们写下每一行:

fwrite($myfile, "\n". $line);
fwrite($myfile, "\n". $date);
fwrite($myfile, "\n". $name);
fwrite($myfile, "\n". $email);
fwrite($myfile, "\n". $line2);

fclose($myfile);

答案 2 :(得分:0)

首先添加您要提交数据的方法postaction

    <form method="post" action="yourpage.php">
      <label>Name</label><input type="text"  name="text"/>
      <label>Email</label><input type="email name="email"" />
      <input type="submit" value="Submit">
    </form>

获取数据。 把POST

$_POST[输入]的名称,当您点击提交按钮时,您将被重定向到另一个页面,数据将在下一页中发布以查看它是否有效尝试使用{回显它{1}}输入名称echo $_POST[