JQuery auto complete for PHP 4

Hi,

I have found a great script which uses the jQuery autocomplete function, which works well when using PHP 5. Unfortunatley the system I am using is PHP 4 and I am attemping to rewrite the script to suit. The form (index.htm) is calling rpc.php which queries MySQL for the list contained in a column. The error I am getting using the rewritten script is:

"Call to a member function on a non-object in"

I have attached the index.htm file, the rpc.php and the original file written for PHP 5.

Any help would be greatly appreciated.

Thank you.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ajax Auto Suggest</title>

<script type="text/javascript" src="jquery-1.2.1.pack.js"></script>
<script type="text/javascript">
	function lookup(inputString) {
		if(inputString.length == 0) {
			// Hide the suggestion box.
			$('#suggestions').hide();
		} else {
			$.post("rpc.php", {queryString: ""+inputString+""}, function(data){
				if(data.length >0) {
					$('#suggestions').show();
					$('#autoSuggestionsList').html(data);
				}
			});
		}
	} // lookup
	
	function fill(thisValue) {
		$('#inputString').val(thisValue);
		setTimeout("$('#suggestions').hide();", 200);
	}
</script>

<style type="text/css">
	body {
		font-family: Helvetica;
		font-size: 11px;
		color: #000;
	}
	
	h3 {
		margin: 0px;
		padding: 0px;	
	}

	.suggestionsBox {
		position: relative;
		left: 30px;
		margin: 10px 0px 0px 0px;
		width: 200px;
		background-color: #212427;
		-moz-border-radius: 7px;
		-webkit-border-radius: 7px;
		border: 2px solid #000;	
		color: #fff;
	}
	
	.suggestionList {
		margin: 0px;
		padding: 0px;
	}
	
	.suggestionList li {
		
		margin: 0px 0px 3px 0px;
		padding: 3px;
		cursor: pointer;
	}
	
	.suggestionList li:hover {
		background-color: #659CD8;
	}
</style>

</head>

<body>


	<div>
		<form>
			<div>
				Type your county:
				<br />
				<input type="text" size="30" value="" id="inputString" onkeyup="lookup(this.value);" onblur="fill();" />
			</div>
			
			<div class="suggestionsBox" id="suggestions" style="display: none;">
				<img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
				<div class="suggestionList" id="autoSuggestionsList">
					&nbsp;
				</div>
			</div>
		</form>
	</div>

</body>
</html>

Open in new window

(PHP 4)
<?php
	
	$con = mysql_connect('localhost', 'USERNAME', 'PASSWORD');

	mysql_select_db("cpark", $con);

	if(!$con) {
		// Show error if we cannot connect.
		echo 'ERROR: Could not connect to the database.';
	} else {
		// Is there a posted query string?
		if(isset($_POST['queryString'])) {
			$queryString = $con->real_escape_string($_POST['queryString']);
			
			// Is the string length greater than 0?
			
			if(strlen($queryString) >0) {
				// Run the query: We use LIKE '$queryString%'
				
				$result = mysql_query("SELECT column_name FROM table WHERE column_name LIKE '$queryString%' LIMIT 10");
				if($result) {
		
					while ($row = mysql_fetch_object($result)) {
						// Format the results, im using <li> for the list, you can change it.
						// The onClick function fills the textbox with the result.
						
	         			echo '<li onClick="fill(\''.$result->column_name.'\');">'.$result->column_name.'</li>';
	         		}
				} else {
					echo 'ERROR: There was a problem with the query.';
				}
			} else {
				// Dont do anything.
			} // There is a queryString.
		} else {
			echo 'There should be no direct access to this script!';
		}
	}
?>

Open in new window

<?php
	
	$db = new mysqli('localhost', 'USERNAME' ,'PASSWORD', 'DATABASE');
	
	if(!$db) {
		// Show error if we cannot connect.
		echo 'ERROR: Could not connect to the database.';
	} else {
		// Is there a posted query string?
		if(isset($_POST['queryString'])) {
			$queryString = $db->real_escape_string($_POST['queryString']);
			
			// Is the string length greater than 0?
			
			if(strlen($queryString) >0) {
				// Run the query: We use LIKE '$queryString%'
				
				$query = $db->query("SELECT column FROM table WHERE your_column LIKE '$queryString%' LIMIT 10");
				if($query) {

					while ($result = $query ->fetch_object()) {
						// Format the results, im using <li> for the list, you can change it.
						// The onClick function fills the textbox with the result.
						
	         			echo '<li onClick="fill(\''.$result->value.'\');">'.$result->value.'</li>';
	         		}
				} else {
					echo 'ERROR: There was a problem with the query.';
				}
			} else {
				// Dont do anything.
			} // There is a queryString.
		} else {
			echo 'There should be no direct access to this script!';
		}
	}
?>

Open in new window

bootneck2222Asked:
Who is Participating?
 
Ray PaseurCommented:
A quote from the PHP.net home page, from back in August.
All PHP users should note that the PHP 5.2 series is NOT supported anymore. All users are strongly encouraged to upgrade to PHP 5.3.7.

To find the end of life for PHP 4 you would have to go into the news archive.  It's not supported any more, not even for security updates.  So rather than trying to fit modern technology into a PHP 4 shell, you might want to put the effort into an upgrade to at least PHP 5.3.7.  At least that is what I would recommend.
0
 
bootneck2222Author Commented:
Thank you Ray for your comment.

Yes it would seem the obvious answer to upgrade, but the system I am using is old (but due for migration in the next 6 months). I need a short term solution if possible.
0
 
bootneck2222Author Commented:
Thanks Ray_Paseur. It looks like my only option is to upgrade.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.