$(document).ready(function(){
$('.submit').on('click', function() {
// e.preventDefault();
var user_id = $('.userid').val();
var username = $('.username').val();
var message = $('.message').val();
var url = $('.comment_form').attr('action');
//e.preventDefault();
$.ajax({
url: url,
type: "POST",
data: {
"user_id": user_id,
"username": username,
"message": message,
"submit_data": $(".submit").val()
},
dataType: 'json',
cache: false,
success: function(data){
//var response = JSON.parse(data);
$('.username').val("");
$('.message').val("");
$('.show').prepend(data.message + data.ip);
//$('.show').append('<p>' + username + '</p><p>' + message + '</p>' + data.ip);
}
});
});
});
PHP
$data = $_POST;
if (isset($data['submit_data'])) {
$user_id = intval($data['user_id']);
$user_name = $data['username'];
$message = $data['message'];
$ip = $_SERVER['REMOTE_ADDR'];
$errors = array();
$result = preg_match("/[<>]/", $message);
if ($message == '' || $result == TRUE) {
$errors[] = 'Нехорошо так делать!';
}
if (empty($data['username']) || empty($data['message']) ) {
$errors[] = 'No Value entered';
}
if (empty($errors)) {
$messages = R::dispense('messages');
$messages->user_id = $user_id;
$messages->username = $user_name;
$messages->message = $message;
$messages->ip = $ip;
$messages->pubdate = date('d.m.Y H:i');
R::store($messages);
header("Location: ".$_SERVER["HTTP_REFERER"]);
$comm = R::getAll('SELECT * FROM messages');
foreach ($comm as $key => $comment) {
$comment = "<div class ='show'><p>". $comment['username'] . "</p>
<p>". $comment['message'] . "</p>";
$comm_ip = "<p>". $comment['ip'] ."</p></div>";
$comments_info = array(
'message' => $comment,
'ip' => $comm_ip
);
echo json_encode($comments);
exit;
}
}else {
$errors = array_shift($errors);