ajax message script problems
Copy link to clipboard
Copied
I have a ajax script that posts messages to mysql and displays the message in on the page via ajax.
I like the function, but I when I refresh the page the message disappears. :'(
PHP INSERT:
if(isset($_POST['message_wall'])){
/* Connection to Database */
include('config.php');
/* Remove HTML tag to prevent query injection */
$message = mysql_real_escape_string($_POST['message_wall']);
$sql = 'INSERT INTO wall (message) VALUES(
"'.$message.'")';
mysql_query($sql);
echo $message;
} else { echo '0'; }
AJAX CODE:
<script type="text/javascript" src="jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("form#submit_wall").submit(function() {
var message_wall = $('#message_wall').attr('value');
$.ajax({
type: "POST",
url: "insert.php",
data: "message_wall="+ message_wall,
success: function(){
$("ul#wall").prepend("<li style='display:none'>"+message_wall+"</li>");
$("ul#wall li:first").fadeIn();
}
});
return false;
});
});
</script>
HTML DISPLAY AREA
<ul id="wall">
</ul>
I have tried using a select statement in insert.php but it does not work.
How can I insert and then select data to display without loosing it on refresh or next visit?
Copy link to clipboard
Copied
Hi
How can I insert and then select data to display without loosing it on refresh or next visit?
The only way to do this is to use a javascript/jQuery cookie, set the cookie as part of your success function.
PZ
Copy link to clipboard
Copied
If I use a cookie, wont it clear on reboot?
I need the message there until the user deletes, or can I auto delete the messages depending on how long its been on the system?
I am just learning ajax and trying to get to grips with it, not very good at it. :'(
Copy link to clipboard
Copied
Hi
Cookies are set for a specific length of time and this can be anything from 1ms to infinity, though the normal appears to be 30 days. You can also delete the cookie or reset it if the site visitor changes anything that you are using a cookie for, you could either delete the old cookie and reset with the new one or get the cookie info and append the new data to it, (if additional data) then re-save.
You appear to be using the jQuery framework if so use the jQuery 'cookie' plug-in for your cookies, see - http://plugins.jquery.com/project/cookie.
PZ
Copy link to clipboard
Copied
That sounds good. Can this also be setup to delete from the database based on the message id?
Copy link to clipboard
Copied
Hi
You would have to call a php, (server side) script with a sql delete function to do this.
PZ
Copy link to clipboard
Copied
thank you.
I will give this a go later.

