我需要一些关于PHP的帮助,并在“?”之后更新URL


I need some assistance with PHP and updating the URL after a "?"

嗨,所以我有一个混合了PHP,HTML和javascript/Ajax的项目。

其中一个要求是"执行程序.php在服务器上传递"?"符号后面的N/V对。我该怎么做?

首先..我不知道NV对是什么,我们得到的阅读链接对它们没有任何说明。我在谷歌上搜索了"Php nv pairs",实际上没有任何相对的东西出现。

我做了一个课堂活动,点击一个按钮后,url 会更新并在"?"之后添加内容,但当然同样的代码不再这样做了。我的教授不善于解释任何事情,让我们遵循他的错别字指示,甚至不完整。

以下是他评论中给我们的代码,试图解释该怎么做:

// startgame function
// sart button pressed  check fields, if OK send to PHP via get request
function startgame(){
//Create the XMLHttpRequest Object
  var xmlhttp;
  // error handling
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
  try {
    xmlhttp = new XMLHttpRequest();
  } catch (e) {
  xmlhttp = false;
}
}
http = xmlhttp;
// The PHP server script, called with N/V pair with GET request
// Using get request the NV pairs follow "?" 
// execute program.php on server passing it the n/v pairs
// that follow the "?"
var url = "program.php?player1name="; 
// for this example, only pass to PHP games played
// "&" separates n/v pairs
// player1name is name of algorithm player 1 is using
player1name = document.JForm.p1name.value;
player2name = document.JForm.p2name.value;
gamesplayed = document.JForm.gamesplayed.value;
sdata = url+player1name+"&gamesplayed="+gamesplayed;
// for a "real" solution, pass more data to PHP about state of game
// send get request to server with n/v pairs of screen data
http.open("GET",sdata, true);  
// register with server what function to return data
http.onreadystatechange = handleHttpResponse;
// send request to server
http.send(null);

}

任何事情都会有所帮助,谢谢!

NV apis 表示"名称/值对"。基本上,在您将在其中使用它们的术语中,它是一个用等号连接到其值的键。例如:username=johnconde . username是键,johnconde是值。与您的代码更密切相关的示例是 gamesplayed=20gamesplayed是键,20是值。

名称/值对常见于查询字符串中,看起来这就是您需要对代码执行的操作。您需要构建一个由名称/值对组成的查询字符串,其中包含 PHP 脚本执行它需要执行的任何操作所需的值。看起来代码已经这样做了,你只需要构建这个JavaScript工作所需的表单。