2013-10-10 88 views
0

我在頁面中有一個表格,它的前兩列來自數據庫中的表格,第三列是複選框,如果用戶知道表格中顯示的單詞的含義,檢查它們 我想以這種方式更新我在數據庫中的表格:對於選中的複選框,插入1到表中的檢查列,否則插入0 如何獲取複選框值並將其插入到數據庫的右側行中 我有此代碼直到如今在php中發送複選框值到數據庫

<?php 
     $con = mysql_connect("localhost", "root", "") 
     or die(mysql_error()); 
     if (!$con) { 
      die('Could not connect to MySQL: ' . mysql_error()); 
     } 
     mysql_select_db("project", $con) 
     or die(mysql_error()); 
     $result = mysql_query("select * from words"); 
     echo "<table border='1'> 
      <tr> 
       <th>word</th> 
       <th>meaning</th> 
       <th>checking</th> 
      </tr>"; 
      while($row = mysql_fetch_array($result)) { 
        echo "<tr>"; 
        echo "<td>" . $row['word'] . "</td>"; 
        echo "<td>". "<div class='hiding' style='display:none'>".$row['meaning']."</div>"."</td>"; 
        echo "<td>"; 
        echo "<input name=\"fahimeh\" type=\"checkbox\" value=\"\"> "; 
        echo "</td>"; 
        echo "</tr>"; 
        } 
     echo "</table>"; 
     mysql_close($con);       
     ?> 
     <button onclick="ShowMeanings()">ShowMeanings</button> 
     <button onclick="feedback()">sendfeedback</button> 

,這是我的javascript代碼:

<script type="text/javascript"> 
     function ShowMeanings(){ 
     var hidings = document.getElementsByClassName('hiding'); 
     for (var i=0; i<hidings.length; i++){ 
     hidings[i].style.display = 'block'; 
     } 

     } 
    </script> 

我不知道我怎麼能寫反饋(),我怎麼能得到複選框值,並將其插入到右排在數據庫

+0

要做到這一點,你」首先需要爲每個複選框設置不同的名稱,否則你不知道哪一個是哪個。 –

+0

[這是你要找的東西。](http://amzn.com/1449319262) – Leng

+0

如果我想設置數據庫中每一行的ID,我應該怎麼做? – fahimeh

回答

0

這應該工作:)

<?php 
     $con = mysql_connect("localhost", "root", "") 
     or die(mysql_error()); 
     if (!$con) { 
      die('Could not connect to MySQL: ' . mysql_error()); 
     } 
     mysql_select_db("project", $con) 
     or die(mysql_error()); 
     $result = mysql_query("SELECT * FROM words"); 
     echo "<table border='1'> 
      <tr> 
       <th>word</th> 
       <th>meaning</th> 
       <th>checking</th> 
      </tr>"; 
      while($row = mysql_fetch_array($result)) { 
        echo "<tr>"; 
        echo "<td>" . $row['word'] . "</td>"; 
        echo "<td>". "<div class='hiding' style='display:none'>".$row['meaning']."</div>"."</td>"; 
        echo "<td>"; 
        echo "<input name=\"fahimeh\" type=\"checkbox\" name='" . $row['id'] . "' value=\"\"> "; 
        echo "</td>"; 
        echo "</tr>"; 
        } 
     echo "</table>"; 
     mysql_close($con);       
     ?> 
     <button onclick="ShowMeanings()">ShowMeanings</button> 
     <button onclick="feedback()">sendfeedback</button> 
+0

name ='checkbox'對於每一行都不是唯一的 如果我想設置name = $ row ['id']我該怎麼辦? – fahimeh

+0

echo「」; –