1

I have a form on my website and need to save the submitted data, the submission date and time in a html file to show the data on a different page.

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

The form looks something like this. After pressing Submit, the data should be written in the file submissions.html, above the existing content, to show the most recent submissions on top.

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

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

What would be the most elegant way to accomplish that in php or javascript?

Gabriel Winkler
  • 315
  • 3
  • 14
  • First of all, u have to name your inputs like this: `` What exacly you want to achieve? Just display last submission? – Yupik Dec 07 '16 at 06:58

3 Answers3

1

I think use 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>

in 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, I'm newbie :3

revelino
  • 11
  • 3
1

it is very simple. you have to create a file called file.php in that file you place following code:

<?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');
}

?>

and in the index.html this content:

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


for explanaition:

this code will count for the current session. its required for *** (int)+1 ***
if(isset($_SESSION['view']))
{
    $_SESSION['view']=$_SESSION['view']+1;
}
else
{
    $_SESSION['view']=1;
}

here you open the file. a stands for append:

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

this code will format your output:

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

and will look like:
image

then we write every line:

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

fclose($myfile);
Blueblazer172
  • 588
  • 2
  • 15
  • 44
0

first add method post and action of where you want to submit the data

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

to get the data. put the POST

$_POST[name of input] when you click the submit button you will be redirected to the other page and the data will be posted in the next page to see it it works try echoing it using echo $_POST[name of input]

Vincent Dapiton
  • 587
  • 1
  • 9
  • 27
  • data need to be save in table then retrieve latest data from the database and display it in tabular form or the way want to display.see for the explanation http://stackoverflow.com/questions/4331353/retrieve-data-from-db-and-display-it-in-table-in-php-see-this-code-whats-wron – kiran gadhvi Dec 07 '16 at 07:05