View Single Post
  #1   (View Single Post)  
Old 23rd September 2008
c0mrade's Avatar
c0mrade c0mrade is offline
Port Guard
 
Join Date: May 2008
Posts: 41
Default PHP and Javascript in action

Hi all,

I'm not sure in which category should I post in but I'll try in this one. Here is my problem, I'm trying to combine 2 scripts that I found useful to me. First one is this , what I'm trying to accomplish is dynamic user look up from my database. The other script is this one , this script echoes entries from database. I have 2 fields in my database "name" and "id" and Here is idea of putting them together.. When I perform search I get some results which are stored within my database then they appear as a list inside <li> html tag.. and when I click on one of the entries from database from "name" I get "id" of that name in another input and that works just fine .. now the other script is listing results also by id .. and so that is they are not working together .. maybe the code will explain better than me.

here is my index.php

Code:
<head>
	<script type="text/javascript" src="prototype.js"></script>
	<script type="text/javascript" src="autocomplete.js"></script>
        <script src="selectuser.js"></script>
	<link rel="stylesheet" type="text/css" href="autocomplete.css" />
</head>
<form>
	<input type="text" name="consumerID" id="consumerID"/>
	<input type="text" name="consumerName"/>
</form>
<script>
	new Autocomplete("consumerName", function() {
		return "index2.php?q=" + this.value;
	});
</script>
Here is my index2.php

Code:
<?php
	$hostname = "localhost";
	$username = "c0mrade";
	$password = "xxx123xxx";
	$dbname = "xxl";

	mysql_connect( $hostname, $username, $password ) or die ("Unable to connect to database!");
	mysql_select_db( $dbname );

	$q = $_GET["q"];
	
	$pagesize = 50;

	mysql_query("set names utf8");

	$sql = "select * from users where locate('$q', name) > 0 order by locate('$q', name), name limit $pagesize";
    
	$results = mysql_query($sql);

	while ($row = mysql_fetch_array( $results )) { 
		$id = $row["id"]; 
		$name = ucwords( strtolower( $row["name"] ) );
		$html_name = preg_replace("/(" . $q . ")/i", "<b>\$1</b>", $name);

		echo "<li onselect=\"this.text.value = '$name'; $('consumerID').value = '$id'; showUser($('consumerID').value) \"><span>$id</span>$name</li>\n";
	}

	mysql_close();
	
?>
Here is getuser.php which should echo results based on id

Code:
<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'c0mrade', 'xxx123xxx');
if (!$con)
 {
 die('Could not connect: ' . mysql_error());
 }

mysql_select_db("xxl", $con);

$sql="SELECT * FROM consumers WHERE id = '".$q."'";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>ID</th>
<th>Lastname</th>
</tr>";

while($row = mysql_fetch_array($result))
 {
 echo "<tr>";
 echo "<td>" . $row['id'] . "</td>";
 echo "<td>" . $row['name'] . "</td>";
 echo "</tr>";
 }
echo "</table>";

mysql_close($con);
?>
Three of javascript I used are attached. Any hints, indications, other approaches? I'm willing to consider any suggestions. Thank you in advance
Attached Files
File Type: txt selectuser.js.txt.txt (900 Bytes, 86 views)
File Type: txt prototype.js.txt.txt (97.6 KB, 61 views)
File Type: txt autocomplete.js.txt.txt (14.6 KB, 88 views)
Reply With Quote