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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
hydroxide
Forum Commoner
Posts: 77
Joined: Mon Jun 05, 2006 9:53 am

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

Post 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>
klarinetking
Forum Commoner
Posts: 59
Joined: Mon Jul 24, 2006 9:43 am

Post 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>
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

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

Post 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
User avatar
hydroxide
Forum Commoner
Posts: 77
Joined: Mon Jun 05, 2006 9:53 am

Post 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.
Post Reply