-1

I am attempting to send a webhook to my discord server when data is inserted into the table, however the function is not being called... Currently it just echos "sendlog()" as shown here: http://prntscr.com/cxqgk5

<!DOCTYPE html>
<html lang="en">
<head>
</head>
	<body>
	<?php
	$token = $_POST["token"];
	$type = $_POST["type"];
	$pid = $_POST["pid"];
	$sid = $_POST["sid"];
	$gid = $_POST["gid"];
	$name = $_POST["name"];
	$players = $_POST["players"];
	$max = $_POST["max"];
	$cdesc = $_POST["cdesc"];
	$sus = $_POST["sus"];

	if($token == 'DS_443'){
		$con =  mysqli_connect("localhost","**","**","**");

		if(mysqli_connect_errno()){
			echo(mysqli_connect_error());
		}
	
		if($type == 'edit' and $players !=NULL and $sid !=NULL){
			$con->query("UPDATE OnlineServers SET ServerCurrent='$players' WHERE ServerID='$sid'");
		} elseif($type == 'remove' and $sid !=NULL){
			$con->query("DELETE FROM OnlineServers WHERE ServerID='$sid'");
		} elseif($type == 'add' and $sid !=NULL and $gid !=NULL and $name!=NULL and $players !=NULL){
			$con->query("INSERT INTO OnlineServers (GameId,GameName,GameMax,ServerCurrent,ServerID,Command) VALUES ('$gid','$name','$max','$players','$sid','TEST')");
	?>
			sendLog();
	<?php
		} elseif($type == 'call'){
			$con->query("INSERT INTO Calls     (Caller,CallerID,CallID,CallDesc,Suspect,SuspectID,ServerID,GameID) VALUES                  ('$pid','$name','$cdesc','$sus')");
		}        
	} else {
	?>
		sendLog();
	<?php
	}
?>

<script>
	function sendLog(){ 
		var hookurl =  "webhookurl"
		let tosend = {
		'Server added;',
		'Game Name: ',
		'Game ID: ',
		'Server ID: ',
		},
		var msgJson = {
			"attachments": [
				{
				"color": "#CC09EF",
				"text": tosend,
				"footer": "Infius Game Logs",
				"footer_icon": "http://deersystems.net/assets/images/nfius.png",
				}
			]
		}
		post(hookurl, msgJson); 
	}
</script>

<script>
	function post(url, jsonmsg){
		xhr = new XMLHttpRequest();
		xhr.open("POST", url, true);
		xhr.setRequestHeader("Content-type", "application/json");
		var data = JSON.stringify(jsonmsg);
		xhr.send(data);
		xhr.onreadystatechange = function() {
			if(this.status != 200){
				alert("ttt");
			}
		}
	}
</script>
</body>

2 Answers 2

2

java script function should be wrapped inside script tags

<script type="text/javascript">
    sendLog();
    </script>

OR

<?php

echo '<script type="text/javascript">',
     'sendLog();',
     '</script>'
;

?>
Sign up to request clarification or add additional context in comments.

Comments

0

You have output sendLog(); whithout script tag so, it will be consider normal text. If you want to call that function then you need it between script tag.

Also, you should have clean formatting of code. And avoid multiple script tag on same pages. add all js code in single script tag.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.