Here I have today what I rewrote from ground up, no longer using any existing code, previously was shout.php but as AJAX took off in the quest to replace that old bugger, I've brought the script up to speed.
No SQL is changed or needed, it uses the existing tables as they are, so hold on to existing shout data so you get a better view of the mod first impression.
So I didn't know where to go for instructions for adding in the markup for stock V3, so I'll leave that up to you. You know your theme.
[color=rgb(226, 80, 65)]* Be sure to update database details in both AJAX scripts in [/color][color=rgb(226, 80, 65)]/ajax/[/color]
Open /themes/default/header.php and add:
Open /themes/default/theme.css and add:
Create /ajax/ajax_shoutbox_post.php
Create /ajax/ajax_shoutbox_get.php
Now upload jQuery.timeago.js from ZIP to /themes/default/js/
This is the HTML block. You can add this anywhere on your site.
That's it, enjoy!
[ATTACH type="full" width="633px" alt="ST-AJAX-Shoutbox.png"]5493[/ATTACH]
It auto refreshes every 10 seconds dynamically. You won't even notice. Press ENTER to post.
No SQL is changed or needed, it uses the existing tables as they are, so hold on to existing shout data so you get a better view of the mod first impression.
So I didn't know where to go for instructions for adding in the markup for stock V3, so I'll leave that up to you. You know your theme.
[color=rgb(226, 80, 65)]* Be sure to update database details in both AJAX scripts in [/color][color=rgb(226, 80, 65)]/ajax/[/color]
Open /themes/default/header.php and add:
<!-- jQuery.min 1.11.1 -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- jQuery.Timeago 1.6.7 -->
<script src="<?php echo $site_config["SITEURL"]; ?>/themes/js/jquery-timeago-1.6.7/jquery.timeago.js"></script>
<!-- CodyHouse Forms Plugin -->
<link rel="stylesheet" href="<?php echo $site_config["SITEURL"]; ?>/js/codyhouse-forms/js/css/reset.css"> <!-- CSS reset -->
<link rel="stylesheet" href="<?php echo $site_config["SITEURL"]; ?>/js/codyhouse-forms/css/style.css"> <!-- Resource style -->
<script src="<?php echo $site_config["SITEURL"]; ?>/js/codyhouse-forms/js/modernizr.js"></script> <!-- Modernizr -->
<script>
function fetchNewShouts() {
$(document).ready(function () {
$.ajax({
url: "/ajax/ajax_shoutbox_get.php",
method: "GET",
dataType: 'JSON',
success: function (data) {
var html_to_append = '';
$.each(data, function (i, item) {
html_to_append +=
'<div class="shoutbox-container"><span class="shoutDate">' +
jQuery.timeago(item.date) +
' <span style="color:#b3b3b3">ago</span></span><span class="shoutUser"><img src="' +
item.avatar +
'" class="shout-avatar" /></span><span class="shoutText">' +
item.message +
'</span></div><br>';
});
$("#shoutbox").html(html_to_append);
$(".shoutbox-container").filter(function () {
return $(this).children().length == 3;
}).filter(':odd').addClass('shoutbox_alt');
$(".shoutbox-container").each(function () {
$(this).parent().nextAll().slice(0, this.rowSpan - 1).addClass('shoutbox_alt');
});
}
});
});
}
fetchNewShouts();
setInterval(function () {
fetchNewShouts()
}, 10000);
// AJAX Shoutbox - POST
$(document).ready(function (e) {
$("#form-shoutbox").on('submit', (function (e) {
e.preventDefault();
$.ajax({
url: "/ajax/ajax_shoutbox_post.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
beforeSend: function () {
$("#posting").html("Posting...").delay(3000).fadeIn().delay(4000).fadeOut();
$("#posting").fadeIn();
$("#err").fadeOut();
},
success: function (data) {
if (data == 'invalid') {
// Invalid data
$("#err").html("Invalid data").fadeIn();
} else {
// Update shoutbox posts after post
$("#refresh-posts").html(data).fadeIn(); // Update element
$("#posting").html("Posted!").delay(2000).fadeIn().delay(1000).fadeOut();
}
},
error: function (e) {
$("#err").html(e).fadeIn();
}
});
}));
});
$('.shoutbox-input').keypress(function (e) {
//e.preventDefault();
if (e.which == 13) {
$('#form-shoutbox').submit();
$("#form-shoutbox")[0].reset();
return false;
}
});
</script>
Open /themes/default/theme.css and add:
.shoutbox-container:nth-child(odd) { /* CSS-way of row backgrounds, no plugin needed ;) */
margin-top: -10px;
padding-top:3px
}
.shoutbox-container:nth-child(odd) { /* CSS-way of row backgrounds, no plugin needed ;) */
margin-bottom: -12px;
}
.shoutDate, .shoutEdit, .shoutUser, .shoutText {
display: table-cell;
padding-top: 3px;
padding-left: 20px;
text-align: left;
vertical-align: middle
}
.shoutText {
width: 533px /* layout fix */
}
.shoutAlt {
background: #FFF;
padding: 5px
}
.shoutbox_alt {
background: #ECECEC
}
.shoutbox_noalt {
background: #FFF;
}
.shout-avatar {
width: 45px;
height: 45px;
border-radius: 50%
}
.shoutbox-form-container {
padding: 7px;
width: 70%;
position: relative
}
.response {
padding-top: 11px
}
Create /ajax/ajax_shoutbox_post.php
<?php
// AJAX Shoutbox - POST
$dbHost = "localhost";
$dbUsername = "username";
$dbPassword = "password";
$dbName = "database";
if (isset($_POST['message'], $_POST['id'])) {
$username = $_POST['username'];
$userid = $_POST['id'];
$message = $_POST['message'];
$sql = 'INSERT INTO shoutbox (msgid, user, message, date, userid) ';
$sql .= ' VALUES (NULL, :username, :message, NOW(), :userid)';
try {
$db = new PDO("mysql:host={$dbHost};dbname={$dbName};charset=utf8", $dbUsername, $dbPassword);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); // Prevent advanced injection by disabling emulated prepared statements
$query = $db->prepare($sql);
$query->bindValue(':username', $username, PDO::PARAM_STR);
$query->bindValue(':userid', $userid, PDO::PARAM_STR);
$query->bindValue(':message', $message, PDO::PARAM_STR);
if ($query->execute()) {
//echo "test";
}
die();
}
catch (Exception $e) {
echo $e->getMessage();
}
}
?>
Create /ajax/ajax_shoutbox_get.php
<?php
// AJAX Shoutbox - GET
$dbHost = "localhost";
$dbUsername = "username";
$dbPassword = "password";
$dbName = "database";
try {
$conn = new PDO("mysql:host={$dbHost};dbname={$dbName};charset=utf8", $dbUsername, $dbPassword);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); // Prevent advanced injection by disabling emulated prepared statements
}
catch (PDOException $e) {
die('Unable to connect to database');
}
$limit = 6;
$stmt = $conn->prepare("SELECT shoutbox.msgid, shoutbox.userid, shoutbox.user, shoutbox.message, shoutbox.date, users.avatar FROM shoutbox LEFT JOIN users ON shoutbox.user = users.username ORDER BY msgid DESC LIMIT :datalimit");
$stmt->bindParam(":datalimit", $limit, PDO::PARAM_INT);
$stmt->execute();
$userData = array();
if ($stmt->rowCount() > 0) {
$usersactive = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($usersactive as $row => $data) {
$data['id'] = $row['msgid'];
$data['value'] = $row['message'];
$data['avatar'] = $row['avatar'];
$data['user'] = $row['user'];
$data['userid'] = $row['userid'];
$data['date'] = $row['date'];
array_push($userData, $usersactive);
}
}
echo json_encode($usersactive, JSON_UNESCAPED_SLASHES);
?>
Now upload jQuery.timeago.js from ZIP to /themes/default/js/
This is the HTML block. You can add this anywhere on your site.
<div id="shoutbox" class="response"></div>
<div class="shoutbox-form-container">
<form class="cd-form floating-labels" name="shoutboxform" id="form-shoutbox" action="/ajax/ajax_shoutbox_post.php" method="post">
<label class="cd-label" for="shout-label">
<i class="material-icons" style="color:rgba(46,168,4,1);vertical-align:middle">textsms</i> @
<span style="color:#252525;font-size:22px" id="posting"><? echo $site_config['SITENAME']; ?></span>
</label>
<input type="text" class="shoutbox-input" name="message" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" id="shout-label" placeholder="Type something..." maxlength="200" style="height:4px;font-size:16px" />
<div style="position:absolute;bottom:25px;left:280px">
<a href="javascript:PopMoreSmiles();"><img src="/images/smilies/grin.png" style="vertical-align:middle;margin-left:10px" /></a>
<a href="javascript:PopMoreSmiles();"><img src="/images/smilies/razz.png" style="vertical-align:middle;margin-left:10px" /></a>
<a href="javascript:PopMoreSmiles();"><img src="/images/smilies/confused.png" style="vertical-align:middle;margin-left:10px" /></a>
<a href="javascript:PopMoreSmiles();"><img src="/images/smilies/cool.png" style="vertical-align:middle;margin-left:10px" /></a>
<a href="javascript:PopMoreSmiles();"><img src="/images/smilies/grin.png" style="vertical-align:middle;margin-left:10px" /></a>
</div>
<input type="hidden" name="id" value="<? echo $CURUSER[" id "]; ?>" required />
<input type="hidden" name="username" value="<? echo $CURUSER[" username "]; ?>" required />
</form>
</div>
That's it, enjoy!
[ATTACH type="full" width="633px" alt="ST-AJAX-Shoutbox.png"]5493[/ATTACH]
It auto refreshes every 10 seconds dynamically. You won't even notice. Press ENTER to post.
