kardanov
kardanov

Reputation: 675

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.

Upvotes: 18

Views: 57567

Answers (4)

user3124464
user3124464

Reputation: 11

If you want to get the data sent to your email address, there are several free services that can do this without installing any server side applications... A PHP or CGI script is still being used but it is hosted by the service, not by you., All you have to do is paste some code into your site and then all submitted data will be sent to your email address.,

A lot of people don't have the know-how to do this on their own, or their hosting service will not allow send-mail to work. Thats why these services exist. And of course most of them are supported by ads that are placed in the email that you get from the form.

Anyway, here is the link for a good service I found. You can also Google "Free Form Processing" to find more.

https://secure.tectite.com/hf/auth/GetStarted?WWWTECTITE

Hope this helps.

Upvotes: 1

Brad Christie
Brad Christie

Reputation: 101604

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: /');

Upvotes: 10

stamat
stamat

Reputation: 1979

How about dumping JSON to a file with PHP and then loading it on request?

How to safely write JSON data to file using PHP

Upvotes: 1

Pez Cuckow
Pez Cuckow

Reputation: 14412

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

}

?>

Upvotes: 3

Related Questions