Php 如何检查表单数据是否有正确答案
所以我有这个: Index.php:Php 如何检查表单数据是否有正确答案,php,jquery,forms,Php,Jquery,Forms,所以我有这个: 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 $s
<?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();
?>
表单中的表是根据网站从数据库获取的数据创建的
例如:
一旦用户点击“Verzenden”,就必须检查他们在输入中填写的内容是否与PLU代码相同。plu代码位于数据库中,连接到产品
如果填写的答案与plu代码不同(第一张图中的示例:有人填写了输入“50”,检查必须不正确,因为根据数据库,plu代码为“30”),则该输入字段的背景色应更改为红色
我试过几次,但都不管用。我希望这里有人能就我应该如何做到这一点提出建议。你不必为我输入代码,只要告诉我怎么做,我就会知道
非常感谢 尝试将其添加到循环代码中,替换最后一个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>';
echo'
';
下面是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>
函数validatePLU(输入,名称)
{
var plu_actual=parseInt(document.getElementById(name+''u plu').value);
var plu_input=parseInt(input.value);
if(plu_实际值!=插头输入)
{
input.style.backgroundColor='#FF0000';
}
其他的
{
input.style.backgroundColor='none';
}
}
不会做任何令人遗憾的事情,也不会出现任何错误。但我喜欢添加一个隐藏的输入字段,其答案是:D EDIT:发现每次我按Submit时,表的顺序也会改变。顺序应该只在页面刷新时改变,如果你点击“opnieuw”,我可以跳吗?