- Home /
Unity form sending empty strings to PHP file
I am trying to send data in a form from Unity to a PHP file using UnityWebRequest. However, I am encountering an error where the form is just sending empty strings of data. The data in the forms is put from an input field, and no matter what u write in the input field, the form just sends empty data. I did debugging in the php file and confirmed that the username and password variables are practically empty.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using UnityEngine.Networking;
public void CallRegister()
{
StartCoroutine(Register());
}
IEnumerator Register()
{
WWWForm form = new WWWForm(); // creating an empty form
// adding fields to the form :
form.AddField("name", nameField.text);
form.AddField("password", passwordField.text);
UnityWebRequest connection = new UnityWebRequest("http://localhost/sqlconnect/register.php", form.ToString());
DownloadHandlerBuffer dH = new DownloadHandlerBuffer();
connection.downloadHandler = dH;
yield return connection.SendWebRequest(); // wait for a response from the PHP file
if (dH.text == "0") // retrieving the echo text from the PHP file, which will be the error codes
{
Debug.Log("User account created successfully");
SceneManager.LoadScene("MainMenu"); // if the php file returns 0, this means there was no error
}
else
{
Debug.Log("User account creation failed. Error #" + dH.text);
}
Answer by daarkies · Mar 14 at 05:22 PM
Alright I fixed the issue, the .Post function finally worked. Why? Because it wasnt supposed to be a constructor. Took out the new, and instead made the line UnityWebRequest connection = UnityWebRequest.Post(url,form); And it worked. The form actually sent it the real data that the user input, thanks for the help
Answer by rh_galaxy · Mar 13 at 11:11 PM
It isn't necessary to create your own download handler and new web request. Just use Post directly. See https://docs.unity3d.com/ScriptReference/WWWForm.html. Doing it that way should look like this:
UnityWebRequest connection = UnityWebRequest.Post("http://localhost/sqlconnect/register.php", form);
yield return connection.SendWebRequest();
Or is there a reason for not using that? In that case you shouldn't send form.ToString() as the data, that is probably why it is empty. But I don't know enough to help you with that.
I was experiencing some errors without those 2 lines of creating and attaching the download handler for some reason, maybe because of something else.
For UnityWebRequest.Post , I get the error CS0426: The type name 'Post' does not exist in the type 'UnityWebRequest'. I dont know why, since as you showed there is a unity doc on that function. Yet for me, unity doesnt recognise it. I thought at first Unity made it obsolete or it got depracated, but maybe its not that.
Also for the form parameter, when the reason I put form.ToString() is because I got the error CS1503: Argument 2: cannot convert from 'UnityEngineWWWForm' to 'string'. I dont know why I am getting these errors since I have seen all these scripts using UnityWebRequest.Post and the form parameter not needing to be casted to a string. Maybe its because the function is UnityWebRequest() and not UnityWebRequest.Post() that it needs a string as the 2nd parameter? Not sure at all
I have imported the UnityEngine.Networking namespace as shown in the previous comment.
UnityWebRequest.Post() should definitely exist. Seems like something is broken in your environment... If you use Visual Studio as the editor you can right click UnityWebRequest and then select "Go to definition" to see the class definition. I use Unity 2021.1, and Post exist for me.
The Post() function can take WWWForm as parameter. Doing ToString() on the form will most likely not be what you want. It is not a cast, it just gives a name-string, not the contents of the form.
Like rh_galaxy said, you should use the static Post method to create a post request. If you really want to create the request yourself, you have a lot things wrong. The constructor of the UnityWebRequest takes the URL and the "method" you want to use as a string, not the post data. So when creating a request manually, you have to pass either "POST"
, "GET"
or any other valid request method. Your actual post data has to be handled with an upload handler. Again using the static Post method does create and setup the request for you.
ps: The static Post and Get method exist from the first day they introduced the UnityWebRequest which was years ago.
Your answer
Follow this Question
Related Questions
How do I properly send binary data (byte[]) to a MySQL database? 4 Answers
multiple formfields, how does php recieve the data? 1 Answer
WWW form string data: best way to encode/escape string data? 1 Answer
WWW Form upload with header not working 2 Answers
Can Unity Open a second game window? 0 Answers