Undefined error. Perhaps stemming from misuse of echo()?
Posted: Tue Jul 25, 2006 8:57 am
With this code, I'm trying to show the contents of the mysql query returned data when the user clicks 'show history'. At this point, JS throws a "foo is undefined" error. I think it's because I didn't escape the quotes with the echo statement, but I'm not sure how I can go about doing that. Actually, I'm not even sure if this is the correct way to go about this, or if that is even the problem. Any insight would be greatly appreciated.
P.S. I stripped out all the unimportant stuff like the <meta> info, etc.
P.S. I stripped out all the unimportant stuff like the <meta> info, etc.
Code: Select all
<?php
$mysql_host = "localhost";
$mysql_user = "root";
$mysql_pass = "";
$mysql_db = "tiki";
$confirm_message = "";
$user = 'jbutler';
// mysql connect
$link = mysql_connect($mysql_host, $mysql_user, '');
if (!$link) die('Could not connect: ' . mysql_error());
$db_selected = mysql_select_db($mysql_db, $link);
if (!$db_selected) die ('Can\'t use tiki : ' . mysql_error());
$clock_history_query = "
SELECT
els_punch_times.user,
els_punch_times.in_time,
els_punch_times.out_time,
els_punch_times.time_stamp,
els_punch_times.status
FROM els_punch_times
WHERE els_punch_times.user = '$user'";
//returns the result set
$clock_history_ret = mysql_query($clock_history_query);
if (!$clock_history_ret) {
die('Invalid query: ' . mysql_error());
}
//parses the array and assigns a result to a new variable
while ($history_array = mysql_fetch_assoc($clock_history_ret)) {
$results[$i++] = $history_array;
}
?>
<title>Web Based Clock In</title>
<script type="text/javascript">
var bar = document.createTextNode(" ");
var foo = document.createTextNode("<?php
foreach($results as $history) {
echo "$history[user], $history[in_time], $history[out_time], $history[time_stamp], $history[status]\n;
} ?>")
</script>
</head>
<body>
<br />
<i onclick="document.body.replaceChild(foo,bar);">Show History</i>
<br />
<script type="text/javascript">
document.body.appendChild(bar);
</script>
</body>
</html>