0
\$\begingroup\$

Is there any way to handle the response for the wwwform we are using add.form method and there is no way to validate the data insertion we cant say the values are entered into database or not is there any way to check the response?

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class LoadOnClicktes : MonoBehaviour {
    public Text box1;
    bool ch=false;

    // Use this for initialization
    string url1="*******/index.php";

    // Update is called once per frame
    public void LoadScene(int score1)
    {
        string kill = box1.text;
        Debug.Log (kill);
        int sco = Player.score;
        Debug.Log (sco);
        WWWForm form = new WWWForm ();
        form.AddField ("name", kill);
        form.AddField ("score",sco);
        WWW WWW = new WWW (url1, form);
        Debug.Log ("Done");
        ch = true;

    }
    /*void OnGUI(){
        if(ch)
        GUI.Window (0, new Rect ((Screen.width / 2) - 150, (Screen.height / 2) - 75
        , 300, 250), ShowGUI, "");
    }
    void ShowGUI(int windowID)
    {
        // You may put a label to show a message to the player

        GUI.Label(new Rect(65, 40, 200, 30), "Score Submitted");

        // You may put a button to close the pop up too

        if (GUI.Button(new Rect(50, 150, 75, 30), "OK"))
        {
            ch = false;
            // you may put other code to run according to your game too
        }

    }*/

}
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Of course you can check the response to your request, you just gotta know when the WWW request has finished. There are different ways to do it, but for your case you could do something like this:

public void LoadScene(int score1)
{
    string kill = box1.text;
    Debug.Log (kill);
    int sco = Player.score;
    Debug.Log (sco);
    StartCoroutine(PublishScores(kill, sco));
}
IEnumerator PublishScores(string kill, int score) {
    WWWForm form = new WWWForm ();
    form.AddField ("name", kill);
    form.AddField ("score",score);
    WWW requestWWW = new WWW (url1, form);
    yield return requestWWW;
    if (requestWWW.responseHeaders.Count > 0)
    {
        foreach (KeyValuePair<string, string> entry in requestWWW.responseHeaders)
        {
            Debug.Log(entry.Value + "=" + entry.Key);
        }
    }
    Debug.Log ("Done");
    ch = true;
}

I didn't test it, but it should work. There you can see all the response headers and do whatever you want with them. Try looking a little more at the documentation to understand how to use it: https://docs.unity3d.com/ScriptReference/WWW.html

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .