将代码从使用GET方法重写为使用POST方法

时间:2019-03-29 07:09:48

标签: javascript php xhtml

出于安全原因,我想将代码从使用GET更改为使用POST。第一个函数(getcurrenthighscoreGet)可以完美工作(返回一个字符串),但是第二个函数(getcurrenthighscorePost)应该给出相同的结果,返回一个零长度的空字符串。有人知道第二个功能出了什么问题吗?

debuggable="%debuggable%" 

被称为的php函数:

function getcurrenthighscoreGet(username) {
  xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function () {
     if (this.readyState == 4 && this.status == 200) {
        document.getElementById("tdScore").innerHTML = parseInt(this.responseText);
    }
  };
  xhttp.open("GET", "getcurrenthighscore.php?q1=" + username, true);
  xhttp.send();
}

function getcurrenthighscorePost(username) {
  var xhttp = new XMLHttpRequest();
  var url = "getcurrenthighscore.php";
  var params = "q1=" + username;
  xhttp.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("tdScore").innerHTML = parseInt(this.responseText);
    }
  };
  xhttp.open("POST", url, true);
  xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhttp.send(params);
}

1 个答案:

答案 0 :(得分:0)

您正在使用$_GET。 POST使用变量$_POST

如果您想同时使用两者,可以执行以下操作:

$var = false;

if(isset($_GET['q1']))$var = $_GET['q1'];
else if(isset($_POST['q1']))$var = $_POST['q1'];

if($var===false) //trigger some error

然后使用$var

if ($stmt->bind_param("s", $var) === false) {
  die('binding parameters failed');
}