Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

eu_Nana

Carregando dados em Select com Ajax

Recommended Posts

Olá pessoal,

 

Estou precisando de uma ajuda.....tenho uma tabela no BD com os seguintes campos: idoficinas, titulo e professor.

 

Tenho um form com 8 selects e pego os dados do BD, preciso fazer o seguinte: qnd selecionar o titulo da oficina em um select no outro select deve exibir o nome do professor....eu fiz porém qnd seleciono uma oficina o outro select some.....não sei oq fiz de errado......vejam o código:

 

form.php

<form action="insere_oficinas.php" method="post">
    
	<table width="100%" border="0">
  		<tr>
    		<td colspan="5"> </td>
    	</tr>
  		<tr>
    		<td colspan="5" align="center">
    		
            </td>
    	</tr>
  		<tr>
    		<td colspan="2"> </td>
    		<td colspan="3"> </td>
  		</tr>
  		<tr>
    		<td colspan="5">
    			
            </td>
    	</tr>
  
  		<tr>
    		<td colspan="5"> </td>
    	</tr>
  		<tr>
    		<td width="8%">1º -</td>
    		<td width="19%">
            <select name="titulo1" id="titulo1">
    			<option value="Selecione">Selecione</option>
      			<? 

				/***************************************************************************************
					Consulta do idparticipantes e nome dos participantes existentes e mostrar no select 
				****************************************************************************************/
				$sql_oficinas= "SELECT idoficinas, titulo from oficinas order by titulo asc";
				$res = mysql_query($sql_oficinas) or die (mysql_error());
		 
		 
				while($row=mysql_fetch_array($res))
		  		{
					echo("<option>" .$row["titulo"]."</option>");
		  		}						
				?>
    		</select>
            
            <script src="http://www.google.com/jsapi"></script>
		<script type="text/javascript">
		  google.load('jquery', '1.3');
		</script>		

		<script type="text/javascript">
		$(function(){
			$('#titulo1').change(function(){
				if( $(this).val() ) {
					$('#professor1').hide();
					$('.carregando').show();
					$.getJSON('oficinas.ajax.php?search=',{titulo1: $(this).val(), ajax: 'true'}, function(j){
						var options = '<option value=""></option>';	
						for (var i = 0; i < j.length; i++) {
							options += '<option value="' + j[i].professor1 + '">' + j[i].professor + '</option>';
						}	
						$('#professor1').html(options).show();
						$('.carregando').hide();
					});
				} else {
					$('#professor1').html('<option value="">– Escolha um estado –</option>');
				}
			});
		});
		</script>
            
            </td>
    		
            <td width="51%">
			<select name="professor1" id="professor1">
      			<option value="">Selecione</option>
      			<!--< 
				/******************************************************************* 
					Consulta do idparticipantes e nome dos participantes existentes
				********************************************************************/
				$sql_oficinas1= "SELECT idoficinas, professor from oficinas order by professor asc";
				$res1 = mysql_query($sql_oficinas1) or die (mysql_error());

				while($row1=mysql_fetch_array($res1))
		  		{
					echo("<option>" .$row1["professor"]."</option>");
				}			
				?>-->
			</select>
			</td>
    		<td colspan="2"> </td>
  		</tr>
  		<tr>
    		<td>2º - </td>
    		
            <td>
			<select name="titulo2" id="titulo2">
       			<option value="Selecione">Selecione</option>
				<? 
		
				/*********************************************************************************************
					Consulta do idparticipantes e nome dos participantes existentes e mostrar na caixa select 
				*********************************************************************************************/
				$sql_oficinas= "SELECT idoficinas, titulo from oficinas order by titulo asc";
				$res = mysql_query($sql_oficinas) or die (mysql_error());
				
				while($row=mysql_fetch_array($res))
				{
					echo("<option>" .$row["titulo"]."</option>");
				}		
				?>
			</select>
			</td>
    		
            <td>
      		<select name="professor2" id="professor2">
        		<option value="Selecione">Selecione</option>
      			<? 
		
				/******************************************************************* 
					Consulta do idparticipantes e nome dos participantes existentes
				********************************************************************/
				$sql_oficinas3= "SELECT idoficinas, professor from oficinas order by professor asc";
				$res3 = mysql_query($sql_oficinas3) or die (mysql_error());

				while($row3=mysql_fetch_array($res3))
		  		{
					echo("<option>" .$row3["professor"]."</option>");
				}
				?>
      		</select>
            </td>
    		
            <td colspan="2"> </td>
  		</tr>
  		
        <tr>
    		<td>3º - </td>
    		<td>
			<select name="titulo3" id="titulo3">
    			<option value="Selecione">Selecione</option>
      			<? 
				/*************************************************************************************** 
					Consulta do idparticipantes e nome dos participantes existentes e mostrar no select 
				***************************************************************************************/
				$sql_oficinas= "SELECT idoficinas, titulo from oficinas order by titulo asc";
				$res = mysql_query($sql_oficinas) or die (mysql_error());

				while($row=mysql_fetch_array($res))
		  		{
					echo("<option>" .$row["titulo"]."</option>");
				}				
				?>
			</select>
			</td>
    		
            <td>
      		<select name="professores3" id="professores3">
        		<option value="Selecione">Selecione</option>
      			<? 
		
				/******************************************************************* 
					Consulta do idparticipantes e nome dos participantes existentes
				********************************************************************/
				$sql_oficinas5= "SELECT idoficinas, professor from oficinas order by professor asc";
				$res5 = mysql_query($sql_oficinas5) or die (mysql_error());

				while($row5=mysql_fetch_array($res5))
		  		{
					echo("<option>" .$row5["professor"]."</option>");
				}			
				?>
      		</select>
            </td>
    		<td colspan="2"> </td>
  		</tr>
  		
        <tr>
    		<td>4º - </td>
    		<td>
    		<SELECT NAME="titulo4" id="titulo4">
           		<option value="Selecione">Selecione</option>
      			<? 
				
				/*** Consulta do idparticipantes e nome dos participantes existentes e mostrar na caixa select ***/
				$sql_oficinas= "SELECT idoficinas, titulo from oficinas order by titulo asc";
				$res = mysql_query($sql_oficinas) or die (mysql_error());
				
				while($row=mysql_fetch_array($res))
		  		{
					echo("<option>" .$row["titulo"]."</option>");
				}				
				?>
			</select>
</td>
    <td>
    <select name="professores4" id="professores4">
        <option value="Selecione">Selecione</option>
      <? 
		/******************************************************************* 
			Consulta do idparticipantes e nome dos participantes existentes
		********************************************************************/
		$sql_oficinas7= "SELECT idoficinas, professor from oficinas order by professor asc";
		$res7 = mysql_query($sql_oficinas7) or die (mysql_error());
		echo $res7;
		while($row7=mysql_fetch_array($res7))
		  {
				echo $row7["professor"];
				echo("<option>" .$row7["professor"]."</option>");
			}
			
/***********************************************************************/				
?>
      </select>     </td>
    <td width="12%"> </td>
    <td width="10%"> </td>
  </tr>
  <tr>
    <td colspan="2"> </td>
    <td> </td>
    <td colspan="2"><input type="submit" name="button" id="button" value="Enviar" /></td>
  </tr>
</table>

</form>

 

oficinas.ajax.php

 

<?php
	header( 'Cache-Control: no-cache' );
	header( 'Content-type: application/xml; charset="utf-8"', true );

	$con = mysql_connect( 'localhost', 'root', '' ) ;
	mysql_select_db( 'teste', $con );

	$titulo1 = mysql_real_escape_string( $_REQUEST['titulo1'] );

	$oficinas = array();

	$sql_oficinas = "SELECT idoficinas, professor
			FROM oficinas
			WHERE titulo=$titulo1
			ORDER BY professor";
	$res = mysql_query( $sql_oficinas );
	while ( $row = mysql_fetch_assoc( $res ) ) {
		$oficinas[] = array(
			'titulo'	=> $row['titulo'],
			'professor'			=> $row['professor'],
		);
	}

	echo( json_encode( $oficinas ) );
	
	?>

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.