Ver Mensaje Individual
  #1 (permalink)  
Antiguo 22/03/2011, 13:36
skull118
 
Fecha de Ingreso: julio-2010
Mensajes: 28
Antigüedad: 13 años, 9 meses
Puntos: 0
Buscar en base de Datos segun un Select

Buenas tardes amigos y maestros de la web y de foros del web, tengo un problema quiero realizar un autocompletado en un input, pero segun un select que tenga, por ejemplo tengo esto:


Código HTML:
Buscar en:
								<select name="campo" id="campo"  onChange="camp(this.value);" onBlur="camp(this.value);">            
								  <?php
								$resultq = mysql_query("SHOW FIELDS FROM `$bd_tabla` " , $link);;
								while($row = mysql_fetch_row($resultq)) {
								?>
								  <option value="<?php echo $row[0]; ?>"><?php echo $row[0];?></option>
								  <?php }
								?>
				  </select>
							Palabra(s): 
							<input type="text" name="palabra" id="palabra" onkeyup="lookup(this.value);" onblur="fill();">
							<br> 

Cuando escriba en palabra que busque en una base de datos, segun el valor del select llamado "campo", este es el javascript que hasta el momento tengo:

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

//Esta parte es la que no me funciona

	function camp(campo){
	$('#campo').document.buscador.campo.value;
	}
</script> 
Lo que no se es como pasar el valor del select llamado campo a este codigo que me hace la autocompletacion:

Código HTML:
<?php
	
	// PHP5 Implementation - uses MySQLi.
	// mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
	$db = new mysqli('localhost', 'mi usuario' ,'', 'contraseña');
	
	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%'
				// The percentage sign is a wild-card, in my example of countries it works like this...
				// $queryString = 'Uni';
				// Returned data = 'United States, United Kindom';
				
				// YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE.
				// eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE '$queryString%' LIMIT 10
				
				$query = $db->query("SELECT nombre_del_cliente FROM mi tabla WHERE nombre_del_cliente LIKE '%$queryString%' LIMIT 10");
				if($query) {
					// While there are results loop through them - fetching an Object (i like PHP5 btw!).
					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.
						
						// YOU MUST CHANGE: $result->value to $result->your_colum
	         			echo '<li onClick="fill(\''.$result->nombre_del_cliente.'\');">'.$result->nombre_del_cliente.'</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!';
		}
	}
?>

Ese codigo si me funciona pero unicamente anclado a el campo "nombre_del_cliente" y unicamente este, la idea es que segun el campo select este supongamos en id, la query se realice en el campo id y no anclado al nombre_del_cliente, algo asi.

Código HTML:
<?php
	
	// PHP5 Implementation - uses MySQLi.
	// mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
	$db = new mysqli('localhost', 'mi usuario' ,'', 'contraseña');
	
$campo= AQUI ES LO QUE NO SE QUE PONER;


	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%'
				// The percentage sign is a wild-card, in my example of countries it works like this...
				// $queryString = 'Uni';
				// Returned data = 'United States, United Kindom';
				
				// YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE.
				// eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE '$queryString%' LIMIT 10
				
				$query = $db->query("SELECT $campo FROM mi tabla WHERE $campo LIKE '%$queryString%' LIMIT 10");
				if($query) {
					// While there are results loop through them - fetching an Object (i like PHP5 btw!).
					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.
						
						// YOU MUST CHANGE: $result->value to $result->your_colum
	         			echo '<li onClick="fill(\''.$result->nombre_del_cliente.'\');">'.$result->nombre_del_cliente.'</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!';
		}
	}
?>
De antemano se los agradezco.