2011-06-20 124 views
-2

我试过everythinng anmad无法让这个程序工作。这是一个国际象棋程序。当用户双击棋盘上的正方形时,单元格应该添加到move_to/move_from变量中的表单中。将变量从Jquery传递到PHP

这里是代码snippits:

<?php 
$results = array(array("Br", "Bn", "Bb", "Bq", "Bk", "Bb", "Bn", "Br"),array("Bp", "Bp", "Bp", "Bp", "Bp", "Bp", "Bp", "Bp"), 
      array("", "", "", "", "", "", "", ""),array("", "", "", "", "", "", "", ""),array("", "", "", "", "", "", "", ""), 
      array("", "", "", "", "", "", "", ""),array("Wp", "Wp", "Wp", "Wp", "Wp", "Wp", "Wp", "Wp"), 
      array("Wr", "Wn", "Wb", "Wq", "Wk", "Wb", "Wn", "Wr")); 

?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 


    <head> 
     <title>Jquery Test</title> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <link type="text/css" href="jq.css" rel="stylesheet" /> 
     <link type="text/css" href="jquery-ui-1.8.9.custom.css" rel="stylesheet" /> 
    </head> 
<!-- Uses Session Variables --> 
     <body> 
      <div id="main"> 
      <fieldset id="main_fieldset"> 
       <table cellspacing="100"> 
        <tbody > 
         <tr> 
          <td class="b1">8</td><td class="d1" id="a8"><?php echo $results[0][0];?> </td><td class="a1" id="b8" ><?php echo $results[0][1];?> 
           </td><td class="d1" id="c8"> <?php echo $results[0][2];?> </td><td class="a1" id="d8"> 
           <?php echo $results[0][3];?> </td><td class="d1" id="e8"><?php echo $results[0][4];?> </td><td class="a1" id="f8"> 
           <?php echo $results[0][5];?> </td> 
           <td class="d1" id="g8"> <?php echo $results[0][6];?> </td><td class="a1" id="h8"> <?php echo $results[0][7];?> </td> 
         </tr> 

剪断..

<script type="text/javascript"> 
$(document).ready(function(){ 
     $('.a1').click(function() { 
      $(this).css("background-color","grey"); 
     }); 
}); 
</script> 

<script type="text/javascript"> 
$(document).ready(function(){ 
     $('.a1').dblclick(function() { 
      $(this).css("background-color","blue"); 
      move_from = $(this).attr("id"); 
      $.post('jq_test.php',move_from); 
      alert(move_from); 
    }); 
}); 

    <p><a href="jq_test.php?move_from">x</a></p> 

<?php 
        echo "this is the variable ";echo $_GET['move_from']; 
        print_r($_GET); 
?> 

喀嚓...

<?php  
    echo <<<HTML 
      <form method="post" action="jq_test.php"> 
      Move From<input type="text" name="move_from"> </input><br /><br /> 
      Move To <input type="text" name="move_to"></input><br /><br /> 
       <input type="submit" value="Enter Move"></input> 

      </form> 
    HTML; 
?> 

    </body> 

</html> 
+0

重复.... – dynamic

+2

@ yes123,在哪里呢? – Neal

+0

搜索将值从jQuery传递给PHP,你会得到很多问题 – dynamic

回答

3

JavaScript是使用$.post(),但你从$_GET检索在PHP中。改为使用PHP切换到$_POST

+0

谢谢。将$ _GET更改为$ _POST。仍然不起作用 – Woffy613

+0

同样,您没有提供POST值的名称。它应该是'$ .post('jq_test.php',{'move_from':move_from});'。现在,这只是一小部分数据(你可以从'php:// input'中读取它)。 –

+0

谢谢。提出你的建议改变。它仍然不起作用 – Woffy613