Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to store data from web site (on server side)

I have very simple web site (which is actually single page), there is one input field and a button.
I need to store data submitted by users somewhere on server side. Perfect way could be simple text file and new lines appended to it after each button click. Log file will be also ok.
As I understand it is not possible with JavaScript itself. I'm looking for easiest solution, preferably with no server-side programming (but if it is required, it should be as easy as possible and work out-of-box). I can use some side service if it could be helpful.
Please help.
Thanks in advance.

UPD. Just want to rephrase the main question. I do not really need to store something on server side. I need to collect some input from users. Is it possible? It would also be ok if it for example will be just sent to my e-mail.

like image 240
kardanov Avatar asked Dec 20 '12 14:12

kardanov


People also ask

Can we store data in web server?

With web storage, web applications can store data locally within the user's browser. Before HTML5, application data had to be stored in cookies, included in every server request. Web storage is more secure, and large amounts of data can be stored locally, without affecting website performance.

What are different ways to store data in browser?

The 3 ways to store data in the browser are Cookies, Local Storage, and Session Storage. Depending on the needs any one of them is used to store data in the browser. In today's article, we will discuss an in-depth comparison between local storage, session storage, and cookies.


2 Answers

For a very simple form-to-server-log script:

Your form:

<form action="save-to-log.php" method="POST">
  <fieldset>
    <legend>Add to log</legend>
    <p>
      Message:
      <textarea name="message"></textarea>
    </p>
    <p>
      <input type="submit" value="SAVE" />
    </p>
  </fieldset>
</form>

Then save-to-log.php

<?php
  $log_file_name = 'mylog.log'; // Change to the log file name
  $message = $_POST['message']; // incoming message
  file_put_contents($log_file_name, $message, FILE_APPEND);
  header('Location: /'); // redirect back to the main site

if it's a unix host you'll need to add 755 permissions to the directory of the log so PHP has access to write to it. Other than that, you'll have a form that keeps appending information to mylog.log.

Follow-Up

If you don't necessarily need it store on the server (you mentioned email) you can use the following instead as the PHP script:

<?php
  $to_email = '[email protected]';
  $subject = 'User feedback from site';
  $message = $_POST['message'];

  // this may need configuring depending on your host. If you find the email isn't
  // being sent, look up the error you're receiving or post another question here on
  // SO.
  mail($to_email, $subject, $message);

  header('Location: /');
like image 167
Brad Christie Avatar answered Oct 06 '22 00:10

Brad Christie


You can't store information on the server without some sort of server side script.

There are two different places to store data, on the client and on the server.

On the client side, there are lots of ways from cookies to Store.js, however it sounds like you want to store the information on the server.

To store on the server you need some sort of application that can receive posts from javascript/http and save them in a file.

In your case a very simple PHP script like the below would be perfect:

<?php

//Was the request (post or get) parameter data supplied?
if(!empty($_REQUEST['data']) {
    $file = 'log.txt';
    $data = $_REQUEST['data']."\n";

    // using the FILE_APPEND flag to append the content to the end of the file
    // and the LOCK_EX flag to prevent anyone else writing to the file at the same time
    file_put_contents($file, $data, FILE_APPEND | LOCK_EX);

}

?>
like image 24
Pez Cuckow Avatar answered Oct 06 '22 01:10

Pez Cuckow