Ver Mensaje Individual
  #8 (permalink)  
Antiguo 22/04/2011, 11:13
Avatar de ginitofl
ginitofl
 
Fecha de Ingreso: diciembre-2006
Ubicación: Lima Perú
Mensajes: 349
Antigüedad: 17 años, 4 meses
Puntos: 22
Respuesta: Mostrar precio en textbox al seleccionar opcion de un select

Hola te hize un pequeño ejemplo de lo que necesitas, estoy usando tres archivos de la siguiente forma:

index.php
consulta.php y
js/ajax.js

como vez vamos a usar ajax para lo que necesitas.

el ajax.js

Código PHP:
function objetoAjax(){
    var 
xmlhttp=false;
    try{
        
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch (
e){
        try{
            
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch (
E){
            
xmlhttp false;
        }
    }
    
    if (!
xmlhttp && typeof XMLHttpRequest!='undefined'){
        
xmlhttp = new XMLHttpRequest();
    }
    return 
xmlhttp;
}

function 
cargaprecio(){
    
divResultado  document.getElementById('principal');//div donde se cargara el resultado
    
atracciones document.getElementById('atracciones').value;//valor del select seleccionado
    
ajax objetoAjax();
    
ajax.open("POST""consulta.php"true);
    
    
ajax.onreadystatechange = function(){
        if (
ajax.readyState==4){
            
divResultado.innerHTML ajax.responseText
        
}
    }
    
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    
ajax.send("atracciones="+atracciones)


el index.php

Código PHP:
<html>
<
head>
<
title>Prueba Ajax</title>
<
script type="text/javascript" src="js/ajax.js"></script>
</head>
<body>
<div>
  <form name="form1" method="post" action="">
    <label>
    <!--realiazas tu consulta para cargar los productos segun la categoria-->
    <select name="atracciones" id="atracciones" onChange="cargaprecio();">
        <option value="0">Seleccione</option>
        <option value="1">Producto 1</option>
        <option value="2">Producto 2</option>
        <option value="3">Producto 3</option>
      </select>
    </label>
 </form>
</div>
<div id="principal">Precio: </div>
</body>
</html> 

y por ultimo el consulta.php

Código PHP:
<?
//recibes la variable en este caso atracciones por que es asi como se llama tu select 
$atracciones=$_REQUEST['atracciones'];
//haces la respectiva consulta a la base de datos y lo muestras en el text box
echo'Precio: <input name=\"precio_atracc\" type=\"text\" size=\"10\" value="100" readonly=\"readonly\"/>';
?>
cualquier consulta no dudes en hacerlo.


Atte.

Gino FL