表格中的复选框选择

时间:2017-07-25 14:47:20

标签: php html db2

我在这里看到过类似的问题但是当我尝试实现它时,代码是不同的。我使用php和html连接到我的db2数据库,从表中提取数据并在html表中显示提取的数据。我正在尝试为我的“删除”功能添加功能。按钮,并希望能够标记一个复选框,该复选框将突出显示表中的选定行。我正在处理我的delete.php但是现在我想实现这个行选择。我在表格的开头找到了一行代码,但是如何突出显示该行?这就是我到目前为止所拥有的:

<html>
<head><title>DB Testing</title></head>




<style>
table{
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 80%;

}
td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}


</style>
<body>

<?php
//db2 express c (v10.5) in local
$database = "db";
$user = "user";
$password = "password";


//create connection
$conn = db2_connect($database, $user, $password);

//check connection
if($conn) {
    //echo "DB2 Connection succeeded.<br><br>";
    } else{
    exit("failed".db2_conn_errormsg());
    }

//select fields from database
$sql = "select 'JUNK', A, B, START_TIME, END_TIME, START_DAY,
END_DAY, C, ID, BUSINESS_AREA, ENVIRONMENT from  testtable where A='D'
and AVAILABILITY = 'Y'";
$stmt = db2_prepare($conn, $sql);

//db2_execute executes a sql statement that was prepared by db2_prepare
if($stmt){
$result = db2_execute($stmt);
if(!$result){
    echo "exec errormsg: " .db2_stmt_errormsg($stmt);
}

//the echos below output the data in an html table  
echo '<table border = 1>';
echo '<thead>';
echo '<tr>';

            echo'<th></th>';
            echo'<th>A</th>';
            echo'<th>B</th>';
            echo'<th>START_TIME</th>';
            echo'<th>END_TIME</th>';
            echo'<th>START_DAY</th>';
            echo'<th>END_DAY</th>';
            echo'<th>C</th>';
            echo'<th>ID</th>';
            echo'<th>BUSINESS_AREA</th>';
            echo'<th>ENVIRONMENT</th>';
echo '</tr>';           
echo '</thead>';
echo '<tbody>';     
while($row = db2_fetch_assoc($stmt)) {


    echo '<tr>';
    echo "<td><input name=\"checkbox[]\" type=\"checkbox\" value=\"".$rows['']. "\" /></td>";
    echo '<td>' . $row['A'] . '</td>';
    echo '<td>' . $row['B'] . '</td>';
    echo '<td>' . $row['START_TIME'] . '</td>';
    echo '<td>' . $row['END_TIME'] . '</td>';
    echo '<td>' . $row['START_DAY'] . '</td>';
    echo '<td>' . $row['END_DAY'] . '</td>';
    echo '<td>' . $row['C'] . '</td>';
    echo '<td>' . $row['ID'] . '</td>';
    echo '<td>' . $row['BUSINESS_AREA'] . '</td>';
    echo '<td>' . $row['ENVIRONMENT'] . '</td>';

    echo '</tr>';   

echo '</tbody>';

}
echo '</table>';
}else {
echo "exec errormsg: ".db2_stmt_errormsg($stmt);
}
db2_close($conn);

?>

<?php
function print_r2($val){
    echo '<pre>';
    print_r($val);
    echo '</pre>';
    }
    ?> 


</body>
</html>

1 个答案:

答案 0 :(得分:0)

您需要一些javascript来完成这个技巧,请参阅代码段:

&#13;
&#13;
function hl(ob){
  if(ob.checked == true){
	  ob.parentNode.parentNode.style.backgroundColor='red';
  }
  if(ob.checked != true){
	  ob.parentNode.parentNode.style.backgroundColor='white';
  }
}
&#13;
<table border=1 cellspacing=0 cellpadding=5>
	<tr>
		<td><input type="checkbox" name="checkbox" value="1" onClick="hl(this)" /></td>
		<td>1</td>
		<td>2</td>
		<td>3</td>
		<td>4</td>
	</tr>
	<tr>
		<td><input type="checkbox" name="checkbox" value="2" onClick="hl(this)" /></td>
		<td>1</td>
		<td>2</td>
		<td>3</td>
		<td>4</td>
	</tr>
	<tr>
		<td><input type="checkbox" name="checkbox" value="3" onClick="hl(this)" /></td>
		<td>1</td>
		<td>2</td>
		<td>3</td>
		<td>4</td>
	</tr>
</table>
&#13;
&#13;
&#13;

修改

<html>
<head>
<title>DB Testing</title>
<script type="text/javascript">
function hl(ob){
  if(ob.checked == true){
      ob.parentNode.parentNode.style.backgroundColor='red';
  }
  if(ob.checked != true){
      ob.parentNode.parentNode.style.backgroundColor='white';
  }
}
</script>
<style>
table{
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 80%;

}
td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}
</style>
</head>
<body>

<?php
//db2 express c (v10.5) in local
$database = "db";
$user = "user";
$password = "password";


//create connection
$conn = db2_connect($database, $user, $password);

//check connection
if($conn) {
    //echo "DB2 Connection succeeded.<br><br>";
    } else{
    exit("failed".db2_conn_errormsg());
    }

//select fields from database
$sql = "select 'JUNK', A, B, START_TIME, END_TIME, START_DAY,
END_DAY, C, ID, BUSINESS_AREA, ENVIRONMENT from  testtable where A='D'
and AVAILABILITY = 'Y'";
$stmt = db2_prepare($conn, $sql);

//db2_execute executes a sql statement that was prepared by db2_prepare
if($stmt){
$result = db2_execute($stmt);
if(!$result){
    echo "exec errormsg: " .db2_stmt_errormsg($stmt);
}
//the echos below output the data in an html table  
echo '<table border = 1>';
echo '<thead>';
echo '<tr>';

            echo'<th></th>';
            echo'<th>A</th>';
            echo'<th>B</th>';
            echo'<th>START_TIME</th>';
            echo'<th>END_TIME</th>';
            echo'<th>START_DAY</th>';
            echo'<th>END_DAY</th>';
            echo'<th>C</th>';
            echo'<th>ID</th>';
            echo'<th>BUSINESS_AREA</th>';
            echo'<th>ENVIRONMENT</th>';
echo '</tr>';           
echo '</thead>';
echo '<tbody>';     
while($row = db2_fetch_assoc($stmt)) {


    echo '<tr>';
    echo "<td><input name=\"checkbox[]\" type=\"checkbox\" value=\"".$rows['']. "\" onClick=\"hl(this)\" /></td>";
    echo '<td>' . $row['A'] . '</td>';
    echo '<td>' . $row['B'] . '</td>';
    echo '<td>' . $row['START_TIME'] . '</td>';
    echo '<td>' . $row['END_TIME'] . '</td>';
    echo '<td>' . $row['START_DAY'] . '</td>';
    echo '<td>' . $row['END_DAY'] . '</td>';
    echo '<td>' . $row['C'] . '</td>';
    echo '<td>' . $row['ID'] . '</td>';
    echo '<td>' . $row['BUSINESS_AREA'] . '</td>';
    echo '<td>' . $row['ENVIRONMENT'] . '</td>';

    echo '</tr>';   

echo '</tbody>';

}
echo '</table>';
}else {
echo "exec errormsg: ".db2_stmt_errormsg($stmt);
}
db2_close($conn);

?>

<?php
function print_r2($val){
    echo '<pre>';
    print_r($val);
    echo '</pre>';
    }
    ?> 


</body>
</html>