Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by daarkies · Mar 13 at 06:27 PM · phpform

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); 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image daarkies · Mar 13 at 11:50 PM 0
Share

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.

avatar image rh_galaxy daarkies · Mar 14 at 01:06 AM 1
Share

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.

avatar image Bunny83 daarkies · Mar 14 at 01:30 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

134 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges