Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get variable from javascript to php?

I want to get a variable from the code value:

/* Link Skor */
Memory.prototype.cekSkor = function(){
    window.location = "./skor/index.php?value=" + this.numMoves;
}

Memory.prototype._winGame = function() {
    var self = this;
    if (this.options.onGameEnd() === false) {
        this._clearGame();
        this.gameMessages.innerHTML = 
            '<h2 class="mg__onend--heading">Keren!</h2>\
            <p class="mg__onend--message">Kamu memenangkan game ini dalam ' + this.numMoves + ' gerakan.</p>\
            <button id="mg__onend--restart" class="mg__button">Ingin Bermain Lagi?</button>\
            <button id="mg__onend--skor" class="mg__button">Cek Skor?</button>';
        this.game.appendChild(this.gameMessages);
        document.getElementById("mg__onend--restart").addEventListener( "click", function(e) {
            self.resetGame();
        });
        document.getElementById("mg__onend--skor").addEventListener( "click", function(e) {
            self.skor();
        });
    } else {
        // run callback
        this.options.onGameEnd();
    }
}

I want get the ( this.numMoves ) to another file is index.php

<?php
        $skornya = $_GET['value'];
        echo $skornya;
?>

when i run the web. i get error The value= not read

enter image description here

What need i do to solve this problem ?

like image 832
Chitoge Kirisaki Avatar asked Apr 20 '26 09:04

Chitoge Kirisaki


1 Answers

no way. =). In PHP just can receive $_GET, $_POST, $_REQUEST, or $data = json_decode( file_get_contents('php://input') );

If you want variable in javascript can be use in PHP. you must post it to server using with XMLHttpRequest in Javascript, ajax in jQuery. hope this help you

like image 123
xuan hung Nguyen Avatar answered Apr 22 '26 00:04

xuan hung Nguyen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!