2011-06-20 126 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