2015-02-07 100 views
0

所以我有這樣的:如何檢查表單數據是否具有正確的答案

的index.php:

<?php 
include "phpscripts/databaseconn.php"; 
$conn = new mysqli('127.0.0.1', 'root', '', 'lidl'); 
if($conn->connect_error) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 

// Query we are going to use on the index page 
$sql = "SELECT * FROM producten ORDER BY Rand()"; 

$result = $conn->query($sql); 

if ($result->num_rows > 0) { 
    // output data of each row 
    while($row = $result->fetch_assoc()) { 
     echo ' <tr> 
        <td><img src="'.$row['Afb'].'"></td> 
        <td>'.$row['Naam'].'</td> 
        <td><input class="input" type="text" autocomplete="off" name="'.$row['Naamlower'].'" placeholder="..."></td> 
       </tr>'; 
    } 
} else { 
    echo "0 results"; 
} 
// Shut down connection with DB 
$conn->close(); 
?> 

表單中的表被創建,基於數據的網站從數據庫中獲取。

例子: Image of fruit, name of fruit, input

只要用戶點擊「Verzenden」必須有一個檢查,如果他們在輸入填充,是一樣的PLU代碼。插件代碼位於數據庫中,連接到產品。

Example from the database

如果填充在答案,是不一樣的PLU代碼(實施例從第一圖像:有人填充在輸入「50」時,檢查必須是不正確的,因爲PLU代碼爲' 30'根據數據庫),該輸入字段的背景顏色應改爲紅色。

我試過了幾件事,但它沒有奏效。我希望這裏有人能夠提出我應該怎麼做。你不必爲我輸入代碼,只要告訴我如何去做,我就知道了!

非常感謝!

+0

我可以在顛簸? – 2015-02-08 21:39:46

回答

0

嘗試添加以下內容到循環代碼替換最後一個TD元素:

echo '<td> 
    <input class="input" type="text" autocomplete="off" name="'.$row['Naamlower'].'" placeholder="..." onchange="validatePLU(this, \''.$row['Naamlower'].'\')"> 
    <input type="hidden" id="'.$row['Naamlower'].'_plu" value="'.$row['PLU'].'"> 
</td>'; 

這裏是JavaScript代碼:

<script type=text/javascript> 
function validatePLU(input, name) 
{ 
    var plu_actual = parseInt(document.getElementById(name+'_plu').value); 
    var plu_input = parseInt(input.value); 

    if(plu_actual != plug_input) 
    { 
     input.style.backgroundColor = '#FF0000'; 
    } 
    else 
    { 
     input.style.backgroundColor = 'none'; 
    } 
} 
</script> 
+0

不會做任何不幸的事情,不會有任何錯誤。但我喜歡添加一個隱藏的輸入字段與答案的想法:D 編輯:發現每當我按提交,表的順序得到改變。訂單隻能在頁面刷新時更改,如果您點擊'opnieuw' – 2015-02-07 20:35:29