SA-MP Forums Archive
javascript. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: Other (https://sampforum.blast.hk/forumdisplay.php?fid=7)
+--- Forum: Everything and Nothing (https://sampforum.blast.hk/forumdisplay.php?fid=23)
+--- Thread: javascript. (/showthread.php?tid=569758)



javascript. - Banditukas - 02.04.2015

Hi,
Code:
$.post("http://mywebsite/server_status.php", {code: "0ay3j5as1Zua55f9T9s2upo"}, function( data ) {
		var value = parseINIString(data);

		if(value["status"])
			document.getElementById("serverStatus").innerHTML = '<span class="label label-success">Server is ON</span>';
		else
			document.getElementById("serverStatus").innerHTML = '<span class="label label-danger">Server is OFF</span>';
		
		if(value["players"] != "")
		{
			output = 'Players: ' + value["players"] + '/' + value["maxplayers"] + '<br>';
			output += 'Version: ' + value["gamemode"] + '<br>';
			document.getElementById("serverInfo").innerHTML	= output;
			
			document.getElementById("serverUpdate").innerHTML = value["lastupdate"];
		}	
		else
		{
			menuHomeTime = 0;	
		}
Please explain structure of this code first thing is:

code: "0ay3j5as1Zua55f9T9s2upo" what is code and for what he is using.

What need to write into .php file to get "status", "players" and another server information.


Re: javascript. - Banditukas - 03.04.2015

I think this is simillar.

https://sampforum.blast.hk/showthread.php?tid=345390

But here he is develoupment only application i need all source codes, php


Re: javascript. - BroZeus - 04.04.2015

Quote:
Originally Posted by Banditukas
Посмотреть сообщение
I don't need to send info to server. I need to parse it to data and get parameters to show how much is players, is server on, off, what is gamemode and etc.
Use this https://sampforum.blast.hk/showthread.php?tid=104299
Now here is example script which shows server info using above api which i mentioned :
PHP код:
<?php
require("SampQueryAPI.php");
$query = new SampQueryAPI('127.0.0.1''7777');//change ip and port here
if(!$query->isOnline())
{
    echo 
"Server Offline";
        exit();
}
$info $query->getInfo();
var_dump($info);
?>



Re: javascript. - Banditukas - 04.04.2015

And how to send this in ajax/jquery? because i need to get this in .js file


Re: javascript. - BroZeus - 04.04.2015

We use javascript and php simultaneously for that.
****** the term "ajax and php".
I prefer passing information in form form of json object, so here is a short example of how to do it :

First lets create the php file to get server info which outputs it in form of json.
PHP код:
//get_info.php
<?php
require("SampQueryAPI.php");
$query = new SampQueryAPI('127.0.0.1''7777');//change ip and port here
if(!$query->isOnline())
{
    echo 
"Server Offline";
    exit();
}
$info $query->getInfo();
echo 
json_encode($info);
?>
Now to get info from php file into javascript we use ajax( note that you need jquery for it)
PHP код:
//javascript
$.ajax({
            
url"get_info.php"
       
}).done(function(data)
       {
            var 
info JSON.parse(data);
            
//now use info.players , info.maxplayers , info.hostname.... etc
            
alert("Game mode of server is : "+info.gamemode);
       } 



Re: javascript. - Banditukas - 04.04.2015

How do that without json ?


Re: javascript. - BroZeus - 04.04.2015

Quote:
Originally Posted by Banditukas
Посмотреть сообщение
How do that without json ?
Why you don't want to use json? You can easily access server information with variables info.gamemode, info.maxplayers ..etc with it if you see my example.


Re: javascript. - Banditukas - 04.04.2015

But others do it without json, just need right write .php file.

Код:
$.post("http://website/login.php", {name: name, password: password, code: "0ay3j5as1Zua55f9T9s2upo"}, function( data )
		{		
			if(data == "-1")
			{
				showWarningMsg("Server is not reachable!");
			}
			else if(data == "-2")
			{
				showWarningMsg("Connection is blocked for a moment!");
			}
else if(data == "0")
			{
showWarningMsg("Invalid password!");
}
I tried in login.php

Код:
<?php
echo "-1";
?>
But i always get Invalid password!. I'am not checking password login, just that things.


Re: javascript. - BroZeus - 04.04.2015

Use json only to pass array or objects or multiple variables..If u have a single variable then you can just echo out that variable.
Try this in javascript and tell what it shows
PHP код:
alert("Data received = "+data); 



Re: javascript. - Banditukas - 04.04.2015

Data is always returning my 0. Json but how i said where i taken this code and what is using it no use it and it work perfectly. When i use them website php file all is working, but i can't to take a look to php code i need create it my self.

And too i want to add that i'am working with android application and i'am going to php file maybe here is need to write other way anwer when working with application.