0

I am trying to do a webpage in which the user types some text into a text field and this text is shown below. I managed to display the text that the user types by adding this text into an array every time they click de submit button. I want this "comments" written by the user to the displayed every time someone loads the webpage, regardless of who typed the comment. So it would be kind of like a guestbook. What I tried to do so far is to save the array in an external file and then load this file when the page reloads. The problem is that I haven't find a way to do this when searching in stackoverflow and google. Do you have any suggestions on how I can do this?

HTML:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <link href="css/reset.css" type="text/css" rel="stylesheet">
    <link href="css/style1.css" type="text/css" rel="stylesheet">
    <title>Speak your mind</title>
</head>
<body>
    <div id="header"><h1>Speak Your Mind</h1></div>
    <div id ="description"><h2>Share with the world whatever is in your mind right now!        </h2></div>
    <div id="writeText"><textarea rows="4" cols="50" id="writeThought"></textarea></div>
    <div id="submitThoughtContainer"><button id="submitYourThought">Submit Your Thought! </button></div>
    <div id="thoughtsContainer"></div>
    <script src="js/script.js"></script>
</body>    
</html>

Javascript:

var thoughts = [];

window.addEventListener('load', function() {
var writeThought = document.getElementById('writeThought');
var recentThought;
var thoughtsContainer = document.getElementById('thoughtsContainer');
var submitYourThought = document.getElementById('submitYourThought');
var thoughtsTemporal = "";

submitYourThought.addEventListener('click', function() {
    recentThought = writeThought.value;
    thoughts.splice(0, 0, recentThought);
    writeThought.value = "";
    thoughtsTemporal = "";
    for(var i = 0; i < thoughts.length; i++) {
        thoughtsTemporal += '<div class="thoughts">'+ thoughts[i] +'</div>';
        thoughtsContainer.innerHTML = "" + thoughtsTemporal;
        }
    });                      
});
4
  • 2
    you shoud have a backend or use localstorage Commented Feb 24, 2014 at 17:38
  • To share data between different clients you need a back-end server. e.g appengine developers.google.com/appengine/docs/python/… Commented Feb 24, 2014 at 17:38
  • Like the others above, you have to use a back end server and then use a server-side language for processing the data (i.e. PHP, Rails, Django, or even Node.js etc) Commented Feb 24, 2014 at 17:58
  • Possible duplicate of How to save my javascript array to a file? Commented Jan 31, 2016 at 19:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.