-1

I'm trying to insert the value of a variable in a database. The project is done entirely in javascript but I need to use php to insert the information in the database.

I have a variable called Score that only indicates the sum of the user's points, it is defined according to a condition.

if(lineCount == corretoBloco){
          var text = BlocklyGames.getMsg('Games_linesOfCode2');
          var medalhaWin = document.getElementById('medalhaWin');
          medalhaWin.innerHTML = 'Ouro';
          window.score = 3;
        }else{
          var text = BlocklyGames.getMsg('Games_linesOfCode2');
          var medalhaWin = document.getElementById('medalhaWin');
          medalhaWin.innerHTML = 'Prata';
          window.score = 2;
        }

This variable is only called case == finish.

case 'finish':
       Maze.scheduleFinish (true);
       BlocklyDialogs.congratulations ();
   }

I need to make case finish, call a function in php that inserts the values of this variable in the database.

Funk Forty Niner
  • 73,764
  • 15
  • 63
  • 131
CodeIn
  • 31
  • 7
  • 2
    You will want to use xmlhttprequest, here is an answer that includes information for that [Send POST data using XMLHttpRequest](https://stackoverflow.com/questions/9713058/send-post-data-using-xmlhttprequest) – imvain2 May 13 '20 at 21:37
  • @imvain32 I need to enter the value of the variable in the database. – CodeIn May 13 '20 at 21:41
  • Does this answer your question? [JavaScript to update MySQL?](https://stackoverflow.com/questions/7060556/javascript-to-update-mysql) – Run_Script May 13 '20 at 21:42
  • 1
    @codein, there are actually two parts to your question. The first is the javascript, which needs to make a XMLHttpRequest/Ajax call to the PHP file for submitting of the data (that is what I linked to), the second part of your question is learning PHP to take a POSTed/GETed variable and connect to a database and insert it. – imvain2 May 13 '20 at 21:52
  • In order to achieve this, research AJAX, WordPress Ajax and PHP $_POST and $_GET. Once you're more familiar with the subject and have a more specific issue, I think the question will be better suited to SO. – syedmh May 13 '20 at 21:59

1 Answers1

1

Resolved by inserting this script in js to send the data to php in a post way.

var xml = new XMLHttpRequest();
    xml.onreadystatechange = function() {
        if( xml.readyState==4 && xml.status==200 ){
            console.log( xml.responseText );
        }
    };

    xml.open("POST", "http://localhost/ajaxTest", false);
    xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xml.send("format=json");
CodeIn
  • 31
  • 7