Jump to content

Send data from client to server


thund3rbird23

Recommended Posts

I have an input label in client side called "fakeInputs.Message" and I want to process this label's value in server side and insert this value to the database it's possible?

Here is how works the input label:

function processFakeInput(key, forcedInput)
	local theInput, inputType, maxChar = getActiveFakeInput(type(forcedInput) == "string" and forcedInput or false)

	if theInput then
		if not fakeInputs[theInput] then
			fakeInputs[theInput] = ""
		end

		if key == "enter" then

		elseif key == "backspace" then
			fakeInputs[theInput] = utf8.sub(fakeInputs[theInput], 1, -2)
		else
			if maxChar > utf8.len(fakeInputs[theInput]) then
				if inputType == "num-only" then
					if tonumber(key) then
						fakeInputs[theInput] = fakeInputs[theInput] .. key
					end
				else
					fakeInputs[theInput] = fakeInputs[theInput] .. key
				end
			end
		end
	end
end

So in server side I need something like this ( ofc, that doesn't work because I don't know how to get the value from client side):

 

dbExec(connection, "INSERT INTO messages VALUES (?)", fakeInputs.Message)

 

Link to comment
  • Scripting Moderators
  On 11/09/2019 at 01:03, thund3rbird23 said:

I have an input label in client side called "fakeInputs.Message" and I want to process this label's value in server side and insert this value to the database it's possible?

Here is how works the input label:

function processFakeInput(key, forcedInput)
	local theInput, inputType, maxChar = getActiveFakeInput(type(forcedInput) == "string" and forcedInput or false)

	if theInput then
		if not fakeInputs[theInput] then
			fakeInputs[theInput] = ""
		end

		if key == "enter" then

		elseif key == "backspace" then
			fakeInputs[theInput] = utf8.sub(fakeInputs[theInput], 1, -2)
		else
			if maxChar > utf8.len(fakeInputs[theInput]) then
				if inputType == "num-only" then
					if tonumber(key) then
						fakeInputs[theInput] = fakeInputs[theInput] .. key
					end
				else
					fakeInputs[theInput] = fakeInputs[theInput] .. key
				end
			end
		end
	end
end

So in server side I need something like this ( ofc, that doesn't work because I don't know how to get the value from client side):

 

dbExec(connection, "INSERT INTO messages VALUES (?)", fakeInputs.Message)

 

Expand  

Take a look:

 

  • Thanks 1
Link to comment
  On 11/09/2019 at 02:06, majqq said:

Take a look:

 

Expand  

Thanks, I did with your suggestion:

 

-- Client-side

local guiText = guiGetText(pin)
triggerServerEvent("check", resourceRoot, guiText) -- pass data

-- Server-side

function checkEvent(text)
	outputChatBox(text)
end
addEvent("check", true)
addEventHandler("check", resourceRoot, checkEvent)

But what about if I have something in server side and I want to display it in client side?

Example (I want to display the "res" in client side):

function LoadMessages()
local query = dbQuery(connection, "SELECT * FROM messages WHERE characterID = 1")
local result = dbPoll(query,-1)	
for x, ad in ipairs( result ) do
local res = result[x]['message']
end
end
addEvent("LoadMsg", true)
addEventHandler("LoadMsg", resourceRoot, LoadMessages)

 

Link to comment
  On 11/09/2019 at 08:29, thund3rbird23 said:

 

function LoadMessages()
local query = dbQuery(connection, "SELECT * FROM messages WHERE characterID = 1")
local result = dbPoll(query,-1)	
for x, ad in ipairs( result ) do
local res = result[x]['message']
end
end
addEvent("LoadMsg", true)
addEventHandler("LoadMsg", resourceRoot, LoadMessages)

 

Expand  

Do not use dbPoll with -1 query time, its will couse server side freeze if one of the query comeback with timeout. You can use the dbQuery's callback function with zero timeout instead of, like this:

 

function LoadMessages()
local query = dbQuery(function(qh)
	result = dbPoll(qh,0)	      
end,connection, "SELECT * FROM messages WHERE characterID = 1")
for x, ad in ipairs( result ) do
local res = result[x]['message']
end
end
addEvent("LoadMsg", true)
addEventHandler("LoadMsg", resourceRoot, LoadMessages)

 

You can read more about there:
https://wiki.multitheftauto.com/wiki/DbQuery

  • Like 1
Link to comment
  On 11/09/2019 at 10:06, Awang said:

Do not use dbPoll with -1 query time, its will couse server side freeze if one of the query comeback with timeout. You can use the dbQuery's callback function with zero timeout instead of, like this:

 


function LoadMessages()
local query = dbQuery(function(qh)
	result = dbPoll(qh,0)	      
end,connection, "SELECT * FROM messages WHERE characterID = 1")
for x, ad in ipairs( result ) do
local res = result[x]['message']
end
end
addEvent("LoadMsg", true)
addEventHandler("LoadMsg", resourceRoot, LoadMessages)

 

You can read more about there:
https://wiki.multitheftauto.com/wiki/DbQuery

Expand  

Thanks!

But I still don't know how to display that in client side.

Link to comment
  • Moderators
  On 11/09/2019 at 11:35, thund3rbird23 said:

But I still don't know how to display that in client side.

Expand  

 

By sending it back

 

Serverside

local something = "something"
triggerClientEvent(player, "sending-something-back" resourceRoot, something)

player = sending it specific to that player.

 

Clientside

addEvent("sending-something-back", true)
addEventHandler("sending-something-back", resourceRoot,
function (something) 
  outputChatBox(something)
end, false)

 

 

 

If you want to work with callbacks instead, you need an enchantment of the feature.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...