Save data to a file and recover them with jquery

Asked

Viewed 468 times

1

I want to simulate a blog article and save a comment purely by Javascript then I have 3 files to accomplish this task:

$("#enviar-comentario").click(function(){
	var nome = $("#nome").val();
	var mensagem = $("#mensagem").val();

	var msg = {
		nome: nome,
		mensagem: mensagem
	}
	var dados = {
		comentario: msg
	}
	$.post("http://127.0.0.1/edsa-direct/blog-inovacoes/js/comentarios", dados, function(){
		console.log("Enviei os dados");
	}).fail(function(){
		console.log("Não deu certo");
	});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div class="comentar col m8 s12 indigo lighten-5">
				<h5 class="center titulo-comentarios">Adicionar um comentário</h5>

				<div class="input-field col s12">
		          <input id="nome" type="text" class="validate">
		          <label for="nome">Nome</label>
		        </div>

				<div class="input-field col s12">
				  	<textarea id="mensagem" class="materialize-textarea" maxlength="150"></textarea>
				    <label for="mensagem">Mensagem</label>
				</div>

				<div class="center">
					<a class="waves-effect waves-light btn" id="enviar-comentario">Comentar</a>
				</div>
</div>

And then, as seen in the js part, I send it to a file called comentarios Which is simply an empty file. The request responds successfully, but when updating the data was not saved there!

What to do to save the data in this file?

  • I don’t understand what you really want to do.

  • After the user fills in the 2 inputs, I want to take their values by the function of the jquery and send by ajax post to that empty file comentarios and save this text in it, to later be manipulated, I just don’t know how to do it. I don’t know if you need to do any preparation in that file comentarios

  • Perhaps there is a better and simpler solution to this. You know localStorage?

  • I was just about to say that

  • I don’t know, but I took a jquery course and learned about AJAX within it. But in the course, you save the information this way and I wanted to test if I can do it the same way.

  • I believe this is only possible in IE using Activex. :/

  • Is there any relation that the course used a Node.js server?

  • It’s likely, since Node.js runs on the server. But I can’t say for sure.

  • Well, then without the other end, there’s no way. Javascript alone doesn’t save the file, otherwise someone could write whatever they wanted by giving a post to google.com for example. There is, in the path you pass the data via post, a server interpreting a file, which will persist that data at some point.

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.