php - Checkbox selection in table -


i've seen similar questions on here code different when try implement it. i'm using php , html connect db2 database, pull data table , display pulled data in html table. i'm trying add functionality 'delete' button , able mark check-box highlight selected row in table. i'm working on delete.php implement row selection. found line of code put @ start of table, how highlight row? here's have far:

<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 database $sql = "select 'junk', a, b, start_time, end_time, start_day, end_day, c, id, business_area, environment  testtable a='d' , availability = 'y'"; $stmt = db2_prepare($conn, $sql);  //db2_execute executes sql statement prepared db2_prepare if($stmt){ $result = db2_execute($stmt); if(!$result){     echo "exec errormsg: " .db2_stmt_errormsg($stmt); }  //the echos below output data in 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> 

you'll need javascript trick, see snippet:

function hl(ob){    if(ob.checked == true){  	  ob.parentnode.parentnode.style.backgroundcolor='red';    }    if(ob.checked != true){  	  ob.parentnode.parentnode.style.backgroundcolor='white';    }  }
<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>

edit

<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 database $sql = "select 'junk', a, b, start_time, end_time, start_day, end_day, c, id, business_area, environment  testtable a='d' , availability = 'y'"; $stmt = db2_prepare($conn, $sql);  //db2_execute executes sql statement prepared db2_prepare if($stmt){ $result = db2_execute($stmt); if(!$result){     echo "exec errormsg: " .db2_stmt_errormsg($stmt); } //the echos below output data in 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> 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -