- Home /
Sending variables to HTML
I've been trying to work out the details of sending a simple int variable to display on a website I manage. I can't tell if I'm bypassing something essential in this process (like setting up a more complicated server), but I've been trying to get WWWForms to work and can't get it to display the variable online correctly.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class WWW_output : MonoBehaviour {
void Start () {
int num;
num = 12;
string url = "http://myurl/test.php";
WWWForm form = new WWWForm ();
form.AddField("var_1", num.ToString());
WWW www = new WWW (url, form);
StartCoroutine (Upload (www));
}
IEnumerator Upload(WWW www){
yield return www;
if (www.error == null){
Debug.Log (www.text);
}
else{
Debug.Log ("unable to update data:" + www.error);
}
}
}
However, when I run the game the information parses correctly and I can see the www.text to display correctly in Unity's console. I'd really like to make sure that I can also just simply display the num.ToString in the browser as well. So I just tried running a simple php template:
<!DOCTYPE HTML>
<HTML>
<BODY>
<?php echo $_POST["var_1"] ?>
</BODY>
</HTML>
But no avail... Any thoughts?
Answer by Ginxx009 · Dec 29, 2017 at 03:55 AM
Please do try this first
Instead of doing this form.AddField("var_1", num.ToString());
all of a sudden just simple do it like this first form.AddField("var_1", var1);
then after that on you php call it like this
PHP
$var1= $_POST["var_1"];
<?php echo (var); ?>
Then if that works your num.ToString();
is wrong??
Hey Paul, thanks for the reply. I definitely implemented this into my php and nothing is visible. I've tried making a dedicated table with the field I'm adding in C#, but still nothing shows up in the browser. I've also made sure to add an encoding argument into the AddField method since passing a string can require such things. However, no luck.
I've also double checked that num.ToString();
works, since this is a common method in C#. Also the thing that is strange is that I get no errors in Unity and when I Debug.Log(www.text);
it displays the correct string in the console — just not in the browser once I make a post request.