Page 1 of 1

Undefined error. Perhaps stemming from misuse of echo()?

Posted: Tue Jul 25, 2006 8:57 am
by hydroxide
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.

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>

Posted: Tue Jul 25, 2006 9:13 am
by klarinetking
Hi,

I'm not sure if this is correct but try this:

Code: Select all

<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>

Re: Undefined error. Perhaps stemming from misuse of echo()?

Posted: Tue Jul 25, 2006 9:17 am
by Luke

Code: Select all

<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>
try that

Posted: Tue Jul 25, 2006 9:22 am
by hydroxide
No dice from either example. Ninja, here is your implementation: http://myels.com/testing/jimmy/punch_in/ninja.php

I'm close, but I've run out of ideas.