Search Unity

unity and php - sending data from game to php file

Discussion in 'Scripting' started by Soumikbhat, Mar 20, 2015.

  1. Soumikbhat

    Soumikbhat

    Joined:
    Nov 23, 2013
    Posts:
    110
    My javascript script reads :

    Code (JavaScript):
    1. function send(data, username) {
    2.     var form = new WWWForm();
    3.     form.AddField("action", "send");
    4.     form.AddField("score", data);
    5.         form.AddField("username", username);
    6.     var url = "http://localhost/server/index.php";
    7.     var w = WWW(url, form);
    8.     yield w;
    9. }
    10. function Update(){
    11. send(999, "phpservisi");    }
    I know it is lame to call send() from Update() but this is just for experimental purposes...

    Now in my php file, I have
    <?php
    if($_REQUEST['action']=="send") {
    $score = $_REQUEST['score'];
    $username = $_REQUEST['username'];
    echo $username; } ?>

    But this does not work...any help would be appreciated..
     
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Does it work if you call the PHP script in your browser?
     
  3. Soumikbhat

    Soumikbhat

    Joined:
    Nov 23, 2013
    Posts:
    110
    It displays :

    Notice: Undefined index: action in D:\xampp\htdocs\server\index.php on line 12

    Line 12 of my server\index.php is
    if($_REQUEST['action']=="send") {
     
  4. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Let me rephrase my question: Does it work if you call the PHP script in your browser with the correct parameters?
     
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
  6. Fiercy Rabbit

    Fiercy Rabbit

    Joined:
    Feb 6, 2016
    Posts:
    1
    Programming with a software in one side and PHP on the other side is not easy for debugging. I'm new on Unity but i've made some Android App linked to a big website. For debugging it's better to build some tools.
    Just built a simple HTML page with a form which submit to your PHP Page. With that, you will be able to send various data and check perfectly your PHP code.
    Then, create a small routine in your PHP script, which save the data in a basic text file (just fopen, fput and fclose).
    Then, work on your Unity script and check the content of the txt file your PHP script has saved.

    In your case, if the reply is "undefined index action", this is because your PHP script don't receive the values. When the data are send to the server, the GET and/or POST array are filled with the value and the index for this array is made from the name of the field.
    Eg an input name="My_Name" filled with "John Doe" will produce and array like:
    $_POST['My_Name'] = "John Doe"
    In your case, the index of the array is missing. So it mean your Unity script dans send the data.

    Concerning the link from JamesLeeNZ, its interessting and will give a good overview. Just avoid using mysql_connect as it's obsolete. Use PDO or mysqli.

    In this code, I suggest having a look at the part with "highscore_url" (in JS) or "post_url" (in the C# part) to understand the contruction of the string which contain the name of the data (which will be the index in the $_REQUEST) and the value.

    Regards
    FR
     
  7. Creer_LLC

    Creer_LLC

    Joined:
    Feb 28, 2017
    Posts:
    15