ICostaEx Designs ICostaEx Designs - 1 month ago 8
Javascript Question

Running a JS script in a PHP file not working

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>

Answer

java script function should be wrapped inside script tags

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

OR

<?php

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

?>