在PHP文件中运行JS脚本不起作用

时间:2016-10-23 01:43:37

标签: javascript php

我正在尝试在将数据插入表格时向我的discord服务器发送webhook,但是该功能未被调用...目前它只是echos" sendlog()"如下所示: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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

java脚本函数应该包含在脚本标记内

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

OR

<?php

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

?>

答案 1 :(得分:0)

您输出 sendLog(); 脚本标记,因此,它将被视为普通文本。如果您想调用该功能,则需要在脚本标记之间使用它。

此外,您应该有干净的代码格式。并避免在同一页面上使用多个脚本标记。在单个脚本标记中添加所有js代码。