需要将变量从JS传递给php

时间:2015-06-09 07:45:12

标签: javascript php

我想为国际象棋生态代码创建一个标题描述:

这是php中名为eco_codes的查找数组:

 <?php
 $eco_tmp = array(
 "A00" => "Uncommon Opening 1.g4, a3, h3, etc.",
 "A01" => "Nimzovich-Larsen Attack 1.b3",
 "A02" => "Bird's Opening 1.f4",
 "A03" => "Bird's Opening 1.f4 d5")
 ?>

我有这样的查找代码(例如GameECO =“A02”):

 <span id="GameECO"></span> 

我想要的是:

 <?php $key = array_search($val['GameECO'], $eco_codes);?>

 <span id="GameECO" title = "<?php echo $key.$val['ECO']></span> 

我该怎么办?这个甚至需要php吗?也许只有js才会这样做?

我的网页是http://communitychessclub.com/basic.php?game=5312

1 个答案:

答案 0 :(得分:0)

假设您在某个变量中有代码,请使用包含

的js文件(ecocodes.js)
  var ecoCodes = { 
      "A00":"Uncommon Opening 1.g4, a3, h3, etc.",
      "A01":"Nimzovich-Larsen Attack 1.b3",
      "A02":"Bird's Opening 1.f4",
      "A03":"Bird's Opening 1.f4 d5"
    } // no comma after the last one

然后你可以使用

<!DOCTYPE html>
<html>
  <head>
    <title>Chess</title>
    <script src="ecocodes.js"></script>
    <script>
      window.onload=function() {
        var eco = "A02";
        document.getElementById("GameECO").innerHTML=eco + ":"+ecoCodes[eco];
      }  
    </script>
  </head>
  <body>  
  ECO: <span id="GameECO"></span>
  </body>
</html>

它的工作原理如下:

  var ecoCodes = { 
      "A00":"Uncommon Opening 1.g4, a3, h3, etc.",
      "A01":"Nimzovich-Larsen Attack 1.b3",
      "A02":"Bird's Opening 1.f4",
      "A03":"Bird's Opening 1.f4 d5"
    } // no comma after the last one

window.onload=function() {
    var eco = "A02";
    document.getElementById("GameECO").innerHTML=eco + ":"+ecoCodes[eco];
}  
ECO: <span id="GameECO"></span>