PDA

View Full Version : php if/then/else causing javascript onmouseover event to hork


questor
05-16-2004, 03:20 AM
When the mouseover event fires on php outputted rows it gives me the err: "'this.field' is undefined". What can I do to fix this?

<script>
function checkIt(j) {
box = eval("document.checkboxform.var" + j);
if (box.checked == false) { box.checked = true; } else { box.checked = false; }
}
</script>

<?
$res = mysql_query("SELECT dept FROM techs WHERE dt=".$dt.";");
$tech_dept = @mysql_result($res, 0);
$_SESSION["tech_dept"] = $tech_dept;
$res = mysql_query("SELECT id FROM depts WHERE dept='".$tech_dept."';");
$tech_dept = @mysql_result($res, 0);
$counter = 1;
$result = mysql_query("SELECT id FROM categories");
$num_rows = mysql_num_rows($result)+1;
$category_number = 1;
while ($category_number < $num_rows)
{
$res = mysql_query("SELECT category FROM categories WHERE id=".$category_number);
$category_name=@mysql_result($res, 0);
echo "<tr><td align=center colspan=2><p>".$category_name."</p></td></tr>";
if ($category_number == "16")
{
$res = mysql_query("SELECT task FROM pri_table WHERE category=".$category_number." AND dept=".$tech_dept." ORDER BY task;");
while ($row = @mysql_fetch_array($res, MYSQL_ASSOC))
{
foreach ($row as $col)
{
echo "<tr bgcolor=\"#F0F0F0\" style=\"cursor:hand\" onmouseover=\"javascript:this.style.backgroundColor='#dddddd';\" onmouseout=\"javascript:document.getElementById(this_field).style.backgroundColor='#F0F0F0'\" onclick=\"checkIt(".$counter."); colorIt()\"><td><input type=checkbox style=\"border=none\" value=\"".$col."\" name=\"var".$counter."\" ></td><td>".$col."</td></tr>";
$counter++;
}
}
}
else
{
$res = mysql_query("SELECT task FROM pri_table WHERE category=".$category_number." ORDER BY task;");
while ($row = @mysql_fetch_array($res, MYSQL_ASSOC))
{
foreach ($row as $col)
{
echo "<tr bgcolor=\"#F0F0F0\" style=\"cursor:hand\" onmouseover=\"javascript:this.style.backgroundColor='#dddddd';\" onmouseout=\"javascript:this.style.backgroundColor='#F0F0F0'\" onclick=\"checkIt(".$counter.");\"><td><input type=checkbox style=\"border=none\" value=\"".$col."\" name=\"var".$counter."\" ></td><td>".$col."</td></tr>";
$counter++;
}
}
}
$category_number++;
}
?>

Roy Sinclair
05-17-2004, 04:22 PM
The "javascript:" you've got in there isn't needed, it doesn't hurt outside of the extra bytes in the page but you can simplify by removing it. I don't think the problem is with the onmouseover event as you suggested though, from the look of the code the problem you're having is on the onmouseout event. For some reason you're looking for a html element with an id value of "this_field" and I suspect that's where you're getting the "undefined" error.

You should make the onmouseout like the onemouseover except for the color.

questor
05-18-2004, 06:20 AM
Thanks! I'm very new to javascript, but I'm liking what I'm seeing! Since this post, I've written a lot of it into my webapp.