Loading data to a server

Hi, is there a standard way to load information to a php script? In the end I want to store something in my MySQL database. So , the idea is to first get the mouseX, mouseY inputs through p5.js -> send to php -> MySQL.

Tagged:

Answers

  • Answer ✓

    Usually it is done via html POST or GET methods. GET is performed by simply adding variables to the php page like this: yourpage.php?mouseX=100&mouseY=200 You can't however just pass variables directly, as javascript only runs in your browser, you need to create a query, like a string above. You may search the internet for examples of "javascript to php" and "GET method"

  • Good! I will! Thanks!

  • Ok! Got into some trouble:

    This is my JS script:

    function serializeObject (obj) {
      var output = '';
      for(var attr in obj) {
        if(obj.hasOwnProperty(attr)) {
          output += attr + '=' + obj + '&';
        }
      }
      return output.slice(0, -1);
    };
    
    var url = 'http://spacej.ru/sample/getMcoordinates.php';
    
    var exampleCoords = {
      x: 31,
      y: 74,
      z: 28
    };
    
    var postData = serializeObject(exampleCoords);
    
    var request = new XMLHttpRequest();
    request.open('POST', url, true);
    
    request.onreadystatechange = function() {
        // request was successful
        if(request.readyState == 4 && request.status == 200) {
            alert(request."the request was successful");
    
        }
    
    
    }
    
    
    request.send(postData);
    

    and this is the PHP code:

    <?php
    
    $x = $_POST ['x'];
    $y = $_POST ['y'];
    $z = $_POST ['z'];
    
    echo $x . $y . $z;
    $dbc = mysqli_connect('some', 'some', 'some', 'some');
    $query = "INSERT INTO rubbish (x, y, z) VALUES ($x, $y, $z)";
    $result = mysqli_query($dbc,$query);    
    
    
    ?>
    

    I get the successful message, but nothing gets inserted into the table. Why is that?

Sign In or Register to comment.