0
The code below allows the user to enter a comment that is returned by the ajax callback to the same page. There is a way via javascript or php that to upgrade the page the inserted comments are not lost?
AJAX.php
<!DOCTYPE html>
<html>
<head>
<title>AJAX</title>
<style>
.comments{
margin-left:400px;
width: 400px;
}
</style>
</head>
<body>
<div class="cadastro">
<form id="form-msg" method="post" enctype="multipart/form-data">
<fieldset>
<p>
<span>Digite seu comentário:</span>
<input type="textbox" id="mensagem">
<button type="submit" id="enviar"> Clique </button>
</p>
</fieldset>
</form>
</div>
<p>Comentários</p>
<span id="aqui"></span>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="ajax.js"></script>
</body>
</html>
AJAX_ACTION.php
<?php
$mensagem = $_REQUEST['mensagem'];
echo "Vem do callback: " . $mensagem;
AJAX.js
$('#form-msg').submit(function(e){
e.preventDefault();
if($('#enviar').val() == 'Enviando...'){
return(false);
}
var mensagem = $('#mensagem').val()
if (mensagem == ''){
alert("Comentário não pode ser vazio")
}
else{
$.ajax({
url: 'ajax_action.php',
type: 'post',
dataType: 'text',
data: {
'mensagem': $('#mensagem').val()
},
success: function(mensagem){
$("#aqui").append("<br>").css("color", "red").append(mensagem)
},
complete: function(){
$("#mensagem").val('')
},
});
}
});
Are you using some kind of database to persist the information?
– Luiz Felipe
You will need to persist this information somewhere as a database, for example.
– Jorge Lima
So without the awkward database?
– Pedro Henrique
Peter try to explain BETTER what you need, because we have already had this question held on the site.
– user148170
With JS you use a cookie. Search by localStorage.
– Sam
Is there a way via javascript or php that when updating the page the inserted comments are not lost? You are not using database for this?
– user148170
@Pedrohenrique, it is even possible to do without database, using local Torage or some cookie but would be visible only to the person who sent the comment... otherwise you write these comments somewhere else, as in a text file on the hard drive. But it’s much more work and it doesn’t make any sense in a real application because the databases are there for it... Without storing the information somewhere, you can’t get it back..
– Jorge Lima
Take a look at the HTML layout. @Pedrohenrique wants comments to be attached to the page just like blog comments. Pedro, if so, only with php and database.
– Augusto Vasques
Possible duplicate of Ajax does not send data to php!
– user148170
@Leandroalfredo I do not believe that this is a duplicate, the code of the questions is the same but the question itself is different. I wonder if the comments inserted in html via javascript could remain there when updating the page, without database use.
– Pedro Henrique