Java Script GUI -> PHP -> MySQL POST ?

Hello Unity Community,

I have a Question how i can exactly post with an GUI.Button username and password to the PHP Script
which query the MySQL DB.I looked and tried some Samples with WWW but i made always mistakes.
What for code and where i should put him :face_with_spiral_eyes:

Here take a look at the small Code:

JavaScript Unity Side

var pass: String = "Password";
var user : String = "Username";

var url = "http://xx.xx.xx.xx/scripts/get.php";


function Update () {
}

function OnGUI () {
  
    user = GUI.TextField (Rect (Screen.width/2-155, Screen.height/2-50, 310, 30), user, 25);
    
    pass = GUI.PasswordField (Rect (Screen.width/2-155, Screen.height/2, 310, 30), pass, "*" [0], 25);
	
    if (GUI.Button(Rect(Screen.width/2-155, Screen.height/2-155, 310, 30),"Submit"))
    Debug.Log("Logged In");
}

PHP CODE

<?php

        $db = mysql_connect('localhost', 'root', '') or die('Could not connect: ' . mysql_error());
        mysql_select_db('MMO') or die('Could not select database');

        // Strings must be escaped to prevent SQL injection attack.
        $user = $_POST['Username'];
        $pass = $_POST['Password'];
      
        $password2 = md5($pass);
        
        // Send variables for the MySQL database class.
        $query = "insert into LoginDB (ID ,Username, Password, MD5, eMail) values ('','$user', '$pass', '$password2', 'eMail');";
        $result = mysql_query($query) or die('Query failed: ' . mysql_error()); 
        echo "registered";
?>',

Thank you for your time

Greetings uNkeLo

Hi, welcome to the forum!

The code looks OK as far as it goes, but of course it doesn’t contain the WWW call to send the data to the server. This belongs in the β€œif” statement in the JS code and might look something like this:-

function OnGUI() {
	...
	
	if (GUI.Button(Rect(Screen.width/2-155, Screen.height/2-155, 310, 30),"Submit")) {
		HandleLogin(user, pass);
	}
	
	...
}

function HandleLogin(username: String, password: String) {
	var form = new WWWForm();
	form.AddField("Username", username);
	form.AddField("Password", password);
	
	var login = new WWW(url, form);
	yield login;
	
	// React to login, check for errors, etc. 
}

Check out the example on the WWWForm manual page for further information about this.

Thanks for your reply , now i understand and it works fine :stuck_out_tongue:

Greetings
uNkeLo