php+jquery+mysql and textarea, please help!
Hi, good afternoon.
I´m trying to load data from mysql into a textarea selecting a information from combobox. I really could do that, but, I had to make a lot of php files, one per textarea. I would like to know if there is a way more easy to do that. Here is the code to you understand better.
- <HTML>
<HEAD>
<TITLE>Clientes</TITLE>
<script type="text/javascript" src="jquery-1.4.1.min.js"></script>
<script type="text/javascript">
//Campo select da Empresa
$(document).ready(function(){
$("select[name=Empresas]").change(function(){
$("select[name=Unidades]").html('<option value="0">Carregando...</option>');
$.post("unidades.php",
{Empresas:$(this).val()},
function(valor){
$("select[name=Unidades]").html(valor);
}
)
- })
})
//Campo select das unidades
$(document).ready(function(){
$("select[name=Unidades]").change(function(){
$("select[name=Contatos]").html('<option value="0">Carregando...</option>');
$.post("contatos.php",
{Unidades:$(this).val()},
function(valor){
$("select[name=Contatos]").html(valor);
}
);
//Textarea das unidades
$.post("unidade/end.php",
{Unidades:$("#unidade option:selected").text()},
function(valor){
$("textarea[name=end]").val(valor);
}
)
$.post("unidade/numend.php",
{Unidades:$("#unidade option:selected").text()},
function(valor){
$("textarea[name=num]").val(valor);
}
)
$.post("unidade/cnpj.php",
{Unidades:$("#unidade option:selected").text()},
function(valor){
$("textarea[name=cnpj]").val(valor);
}
)
$.post("unidade/cidade.php",
{Unidades:$("#unidade option:selected").text()},
function(valor){
$("textarea[name=cidade]").val(valor);
}
)
$.post("unidade/estado.php",
{Unidades:$("#unidade option:selected").text()},
function(valor){
$("textarea[name=estado]").val(valor);
}
)
$.post("unidade/cep.php",
{Unidades:$("#unidade option:selected").text()},
function(valor){
$("textarea[name=cep]").val(valor);
}
)
$.post("unidade/bairro.php",
{Unidades:$("#unidade option:selected").text()},
function(valor){
$("textarea[name=bairro]").val(valor);
}
)
$.post("unidade/ie.php",
{Unidades:$("#unidade option:selected").text()},
function(valor){
$("textarea[name=ie]").val(valor);
}
)
- })
})
//textarea dos contatos
$(document).ready(function(){
$("select[name=Contatos]").change(function(){
$.post("contato/nome.php",
{Contatos:$("#contato option:selected").text()},
function(valor){
$("textarea[name=nome]").val(valor);
}
)
$.post("contato/email.php",
{Contatos:$("#contato option:selected").text()},
function(valor){
$("textarea[name=email]").val(valor);
}
)
$.post("contato/dep.php",
{Contatos:$("#contato option:selected").text()},
function(valor){
$("textarea[name=dep]").val(valor);
}
)
$.post("contato/fax.php",
{Contatos:$("#contato option:selected").text()},
function(valor){
$("textarea[name=fax]").val(valor);
}
)
$.post("contato/cel.php",
{Contatos:$("#contato option:selected").text()},
function(valor){
$("textarea[name=cel]").val(valor);
}
)
$.post("contato/tel.php",
{Contatos:$("#contato option:selected").text()},
function(valor){
$("textarea[name=tel]").val(valor);
}
)
- })
})
</script>
</HEAD>
<BODY>
<form action="" method="post">
<select name="Empresas">
<option value="0"> Escolha a Empresa</option>
<?
require_once("conecta.php");
$sql="SELECT * from empresas ORDER BY Empresas ASC";
$qr=mysql_query($sql) or die (mysql_error());
while($ln=mysql_fetch_assoc($qr)){
echo '<option value="'.$ln['id'].'">'.$ln['Empresas'].'</option>';
}
?>
</select>
<select name="Unidades" id="unidade">
<option value="0" disable="disable">Escoha a unidade</option>
</select>
<select name="Contatos" id="contato">
<option value="0" disable="disable">Escolha o contato</option>
</select><br>
<h2>Contato</h2><hr>
<table border=0>
<tr>
<td>Nome: <textarea readonly name="nome" cols="25" rows="1" style="overflow:hidden"></textarea></td>
<td>E-mail: <textarea readonly name="email" cols="25" rows="1" style="overflow:hidden"></textarea></td>
<td>Departamento: <textarea readonly name="dep" cols="25" rows="1" style="overflow:hidden"></textarea></td>
</tr>
<tr>
<td>Fax: <textarea readonly name="fax" cols="25" rows="1" style="overflow:hidden"></textarea></td>
<td>Celular: <textarea readonly name="cel" cols="25" rows="1" style="overflow:hidden"></textarea></td>
<td> Telefone: <textarea readonly name="tel" cols="25" rows="1" style="overflow:hidden"></textarea></td>
</tr>
</table>
<h2>Unidade</h2><hr><br>
<table border=0>
<tr>
<td>Endereço: <textarea readonly name="end" cols="25" rows="1" style="overflow:hidden"></textarea></td>
<td>Bairro: <textarea readonly name="bairro" cols="25" rows="1" style="overflow:hidden"></textarea></td>
<td>Nº: <textarea readonly name="num" cols="5" rows="1" style="overflow:hidden"></textarea></td>
</tr>
<tr>
<td>Cidade: <textarea readonly name="cidade" cols="25" rows="1" style="overflow:hidden"></textarea></td>
<td>Estado: <textarea readonly name="est" cols="25" rows="1" style="overflow:hidden"></textarea></td>
</tr>
<tr>
<td>CEP: <textarea readonly name="cep" cols="25" rows="1" style="overflow:hidden"></textarea></td>
<td>CNPJ: <textarea readonly name="cnpj" cols="25" rows="1" style="overflow:hidden"></textarea></td>
<td>IE: <textarea readonly name="ie" cols="25" rows="1" style="overflow:hidden"></textarea></td>
</tr>
</table>
</form>
</BODY>
</HTML>
And this is the code from one of the php file that jquery reffers:
end.php
- <?
require_once("conecta.php");
$Unidades=$_POST['Unidades'];
$sql="SELECT * FROM unidades WHERE Unidades = '$Unidades'";
$qr=mysql_query($sql) or die (mysql_error());
if (mysql_num_rows($qr) > 0){
- while($ln = mysql_fetch_assoc($qr)){
echo $ln['End'];
}
}
?>
Sorry about bad english, I´m from Brazil.
Thanks.