简体   繁体   中英

Adding onclick function on a dynamically added table row

I have a page which displays records in a table and I am able to do that but after adding a code so a highlighted table row will change color, I am now getting error instead.

$result = mysqli_query($con,"SELECT * FROM mydb WHERE `Main Type`='main1' AND `DB Type`='Active' ORDER BY `Record ID`");

echo "<table border='1' style='width:100%; font-family:arial,Serif;font-style:regular;font-size:12px; color:black' CELLPADDING='1' CELLSPACING='0'>
<tr>
<th>Record ID</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>County</th>
<th>Price</th>
<th>Bed</th>
<th>Bath</th>
<th>Square Foot</th>
<th>Year Built</th>
<th>As Is Value</th>
<th>DB Type</th>
<th>Main Type</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr onclick='toggle(this)'>";
  echo "<td>" . $row['Record ID'] . "</td>";
  echo "<td>" . $row['Address'] . "</td>";
  echo "<td>" . $row['City'] . "</td>";
  echo "<td>" . $row['State'] . "</td>";
  echo "<td>" . $row['Zip Code'] . "</td>";
  echo "<td>" . $row['County'] . "</td>";
  echo "<td>" . $row['Price'] . "</td>";
  echo "<td>" . $row['Bed'] . "</td>";
  echo "<td>" . $row['Bath'] . "</td>";
  echo "<td>" . $row['Square Foot'] . "</td>";
  echo "<td>" . $row['Year Built'] . "</td>";
  echo "<td>" . $row['As Is Value'] . "</td>";
  echo "<td>" . $row['DB Type'] . "</td>";
  echo "<td>" . $row['Main Type'] . "</td>";
  echo "</tr>";
  }
echo "</table><br>";

echo "<script type='text/javascript'>";
echo "function toggle(it) { if ((it.style.backgroundColor == 'none') || (it.style.backgroundColor == '')){it.style.backgroundColor = 'yellow';}}";
echo "</script>"


mysqli_close($con);

Basically my main goal is if the user clicks on a row, the entire row will change color to indicate it is being selected. My more advance goal is a mousemove instead of onclick. I'm looking for the easiest and less complicated way to do this, I have this feeling that the solution is simple. I hope you guys can help.

The error I'm getting is

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/u560877965/public_html/hud.php on line 59

You have missed semicolon from after string in code.

Change this:

echo "</script>"

to this:

echo "</script>";
//              ^ here (for those who can't see it)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM