Browse code

Convert form submit to AJAX

Prevents resubmit on page reload

Brodur authored on 2020/05/16 12:32:01 • GitHub committed on 2020/05/16 12:32:01
Showing 1 changed files
... ...
@@ -4,7 +4,8 @@ if(!defined("_CHARSET")) exit( );
4 4
 if(file_exists(_BASEDIR."blocks/shoutbox/{$language}.php")) include_once(_BASEDIR."blocks/shoutbox/{$language}.php");
5 5
 else include_once(_BASEDIR."blocks/shoutbox/en.php");
6 6
 $content = "";
7
-if(isset($_POST['shout']) && (isMEMBER || !empty($blocks['shoutbox']['guestshouts']))) {
7
+if(isset($_POST['shouthidden']) && (isMEMBER || !empty($blocks['shoutbox']['guestshouts']))) {
8
+	http_response_code(202);
8 9
 	if(isMEMBER) $shout_name = USERUID;
9 10
 	else {
10 11
 		$shout_name = trim(descript($_POST['shoutname']));
... ...
@@ -24,7 +25,23 @@ if(isset($_POST['shout']) && (isMEMBER || !empty($blocks['shoutbox']['guestshout
24 25
 
25 26
 }
26 27
 if(isMEMBER || !empty($blocks['shoutbox']['guestshouts'])) {
27
-	$content = "<script language=\"javascript\" type=\"text/javascript\">
28
+	$content = "<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js\"></script>
29
+	<script language=\"javascript\" type=\"text/javascript\">
30
+	$(document).ready(function() {
31
+		$(function () {
32
+		  $('#shout').click( function () {
33
+			$.ajax({
34
+			  type: 'post',
35
+			  url: $('#shoutbox').attr('action'),
36
+			  data: $('#shoutbox').serialize(),
37
+			  success: function () {
38
+				location.reload();
39
+				$('#shout_message').val(\"\");
40
+			  }
41
+			});
42
+		  });
43
+		});
44
+	});
28 45
 var shoutend = '"._SHOUTEND."';
29 46
 function ismaxlength(obj){
30 47
 var mlength = 200
... ...
@@ -40,7 +57,8 @@ if (document.shoutbox.shout_message.value.length >= mlength) {
40 57
 	if(!isMEMBER) $content .= "<div><label for='shoutname'>"._NAME.":</label> <input type='text' name='shoutname' value='' style='display: block; width: 90%; margin: 0 auto;' maxlength='30'></div>";
41 58
 	else $content .= "<div><span class='label'>"._NAME.":</span> ".USERPENNAME."</div>";
42 59
 	if(!isMEMBER && $captcha) $content .= "<div><label for='userdigit'>"._CAPTCHANOTE."</label><input MAXLENGTH=5 SIZE=5 name=\"userdigit\" type=\"text\" value=\"\"><div style='text-align: center;'><img width=120 height=40 src=\""._BASEDIR."includes/button.php\" style=\"border: 1px solid #111;\"></div></div>";
43
-	$content .= "<div><label for='shout_MESSAGE'>"._SHOUT.":</label> <textarea name='shout_message' class='mceNoEditor' id='shout_message' rows='4' cols='15' maxlength='200' style='display: block; width: 90%; margin: 0 auto;' onkeyDown='return ismaxlength(this)'></textarea></div> <input type='submit' name='shout' id='shout' value='"._SHOUT."'> <input size='1' class='small' type='text' id='counter' value='200'></form></div>";
60
+	$content .= "<div><label for='shout_MESSAGE'>"._SHOUT.":</label> <textarea name='shout_message' class='mceNoEditor' id='shout_message' rows='4' cols='15' maxlength='200' style='display: block; width: 90%; margin: 0 auto;' onkeyDown='return ismaxlength(this)'></textarea></div> <input type='button' name='shout' id='shout' value='"._SHOUT."'> <input size='1' class='small' type='text' id='counter' value='200'> <input type='hidden' name='shouthidden' id='shouthidden' value='"._SHOUT."'>
61
+	</form></div>";
44 62
 }
45 63
 if(isset($blocks['shoutbox']['shoutlimit'])) $shoutlimit = $blocks['shoutbox']['shoutlimit'];
46 64
 else $shoutlimit = 10;
... ...
@@ -63,4 +81,4 @@ if(dbnumrows($shouts) != 0) {
63 81
 else $content .= write_message(_NOSHOUTS);
64 82
 $content .= "</div>";
65 83
 if($totalshouts > $shoutlimit) $content .= write_message("<a href='"._BASEDIR."blocks/shoutbox/archive.php'>"._SHOUTARCHIVE."</a>");
66
-?>
67 84
\ No newline at end of file
85
+?>
Browse code

Initial uplad w/ php7 fixes

Rainer Volkrodt authored on 2016/03/12 15:54:02
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,66 @@
1
+<?php
2
+if(!defined("_CHARSET")) exit( );
3
+
4
+if(file_exists(_BASEDIR."blocks/shoutbox/{$language}.php")) include_once(_BASEDIR."blocks/shoutbox/{$language}.php");
5
+else include_once(_BASEDIR."blocks/shoutbox/en.php");
6
+$content = "";
7
+if(isset($_POST['shout']) && (isMEMBER || !empty($blocks['shoutbox']['guestshouts']))) {
8
+	if(isMEMBER) $shout_name = USERUID;
9
+	else {
10
+		$shout_name = trim(descript($_POST['shoutname']));
11
+		if(isNumber($shout_name)) $shout_name = _GUEST;
12
+		if($captcha && !captcha_confirm()) $shout_name = false;
13
+	}
14
+	$shout_message = trim($_POST['shout_message']);
15
+	$shout_message = preg_replace("/^(.{200}).*$/", "$1", $shout_message);
16
+	$shout_message = preg_replace("/[^\s]{25}/", "$1\n", $shout_message);
17
+	$search = array("\"", "'", "\\", '\"', "\'", "<", ">", "&nbsp;", "\n");
18
+	$replace = array("&quot;", "&#39;", "&#92;", "&quot;", "&#39;", "&lt;", "&gt;", " ", " ");
19
+	$shout_message = str_replace($search, $replace, replace_naughty(trim($shout_message)));
20
+	$shout_message = str_replace("\n", "<br />", $shout_message);
21
+	if(!empty($shout_name) && !empty($shout_message)) {
22
+		dbquery("INSERT INTO ".TABLEPREFIX."fanfiction_shoutbox(`shout_name`, `shout_message`, `shout_datestamp`) VALUES('$shout_name', '$shout_message', '".time()."')");
23
+	}
24
+
25
+}
26
+if(isMEMBER || !empty($blocks['shoutbox']['guestshouts'])) {
27
+	$content = "<script language=\"javascript\" type=\"text/javascript\">
28
+var shoutend = '"._SHOUTEND."';
29
+function ismaxlength(obj){
30
+var mlength = 200
31
+shout_messagelength = document.shoutbox.shout_message.value.length;
32
+document.shoutbox.counter.value = 200 - shout_messagelength;
33
+if (document.shoutbox.shout_message.value.length >= mlength) {
34
+   alert(shoutend);
35
+   document.shoutbox.shout_message.value = document.shoutbox.shout_message.value.substr(0, mlength)
36
+}
37
+}
38
+
39
+</script><div id='shoutbox_container' style='width: 100%;'>\n<form name='shoutbox' id='shoutbox' method='POST' action='".$_SERVER['SCRIPT_NAME'].($_SERVER['QUERY_STRING'] ? "?".$_SERVER['QUERY_STRING']  : "")."'>\n";
40
+	if(!isMEMBER) $content .= "<div><label for='shoutname'>"._NAME.":</label> <input type='text' name='shoutname' value='' style='display: block; width: 90%; margin: 0 auto;' maxlength='30'></div>";
41
+	else $content .= "<div><span class='label'>"._NAME.":</span> ".USERPENNAME."</div>";
42
+	if(!isMEMBER && $captcha) $content .= "<div><label for='userdigit'>"._CAPTCHANOTE."</label><input MAXLENGTH=5 SIZE=5 name=\"userdigit\" type=\"text\" value=\"\"><div style='text-align: center;'><img width=120 height=40 src=\""._BASEDIR."includes/button.php\" style=\"border: 1px solid #111;\"></div></div>";
43
+	$content .= "<div><label for='shout_MESSAGE'>"._SHOUT.":</label> <textarea name='shout_message' class='mceNoEditor' id='shout_message' rows='4' cols='15' maxlength='200' style='display: block; width: 90%; margin: 0 auto;' onkeyDown='return ismaxlength(this)'></textarea></div> <input type='submit' name='shout' id='shout' value='"._SHOUT."'> <input size='1' class='small' type='text' id='counter' value='200'></form></div>";
44
+}
45
+if(isset($blocks['shoutbox']['shoutlimit'])) $shoutlimit = $blocks['shoutbox']['shoutlimit'];
46
+else $shoutlimit = 10;
47
+if(!empty($blocks['shoutbox']['shoutdate'])) $shoutdate = $blocks['shoutbox']['shoutdate'];
48
+else $shoutdate = $dateformat." ".$timeformat;
49
+$shouts = dbquery("SELECT shouts.*, "._PENNAMEFIELD." as penname FROM ".TABLEPREFIX."fanfiction_shoutbox as shouts LEFT JOIN "._AUTHORTABLE." ON "._UIDFIELD." = shouts.shout_name ORDER BY shout_datestamp DESC");
50
+$totalshouts = dbnumrows($shouts);
51
+$shouts = dbquery("SELECT shouts.*, "._PENNAMEFIELD." as penname FROM ".TABLEPREFIX."fanfiction_shoutbox as shouts LEFT JOIN "._AUTHORTABLE." ON "._UIDFIELD." = shouts.shout_name ORDER BY shout_datestamp DESC LIMIT 0, ".$shoutlimit);
52
+$content .= "<div id='shoutlist' style='height: 200px; overflow: auto;'>";
53
+if(dbnumrows($shouts) != 0) {
54
+	while($shout = dbassoc($shouts)) {
55
+		if(isNumber($shout['shout_name']) && isset($shout['penname'])) $shoutname = "<a href='"._BASEDIR."viewuser.php?uid=".$shout['shout_name']."'>".$shout['penname']."</a>";
56
+		else if(isset($shout['shout_name'])) $shoutname = $shout['shout_name'];
57
+		else $shout = _GUEST; // Just in case.
58
+		$content .= "<span class='sbname'>$shoutname</span><br /><span class='sbdatetime'>".date("$shoutdate", $shout['shout_datestamp']);
59
+		if(isADMIN) $content .= " [<a href='"._BASEDIR."admin.php?action=blocks&amp;admin=shoutbox&amp;shout_id=".$shout['shout_id']."' class='sbadmin'>"._EDIT."</a>]";
60
+		$content .= "</span><br />\n<span class='sbshout'>".stripslashes($shout['shout_message'])."</span><br />";
61
+	}
62
+}
63
+else $content .= write_message(_NOSHOUTS);
64
+$content .= "</div>";
65
+if($totalshouts > $shoutlimit) $content .= write_message("<a href='"._BASEDIR."blocks/shoutbox/archive.php'>"._SHOUTARCHIVE."</a>");
66
+?>
0 67
\ No newline at end of file