• 0
Sign in to follow this  
Guest daniel santos

Problema Com Formulario

Question

Guest daniel santos

Ae to tentando fazer um formulario que pegue o cep pelo banco e preencha automaticamente os campos, mas estou tendo problemas ele limpa os outros campos ao fazer isto. vou postar o codigo

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/index.dwt.php" codeOutsideHTMLIsLocked="false" -->

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<!-- InstanceBeginEditable name="doctitle" -->

<title>:: Manivela ::</title>

<!-- InstanceEndEditable -->

<!-- InstanceBeginEditable name="head" -->

<!-- InstanceEndEditable -->

<style type="text/css">

<!--

body {

margin-left: 0px;

margin-top: 0px;

margin-right: 0px;

margin-bottom: 0px;

background-image: url(../../img/bg.png);

}

-->

</style>

<link href="../../style/default.css" rel="stylesheet" type="text/css" />

<style type="text/css">

<!--

a:link {

color: #666666;

text-decoration: none;

}

a:visited {

text-decoration: none;

color: #666666;

}

a:hover {

text-decoration: underline;

color: #999999;

}

a:active {

text-decoration: none;

color: #666666;

}

-->

</style>

<script type="text/JavaScript">

<!--

function MM_swapImgRestore() { //v3.0

  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a)&&x.oSrc;i++) x.src=x.oSrc;

}

function MM_preloadImages() { //v3.0

  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();

    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)

    if (a.indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a;}}

}

function MM_findObj(n, d) { //v4.01

  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {

    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}

  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[n];

  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers.document);

  if(!x && d.getElementById) x=d.getElementById(n); return x;

}

function MM_swapImage() { //v3.0

  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)

  if ((x=MM_findObj(a))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}

}

//-->

</script>

</head>

<body onload=MM_preloadImages('../../img/btn_home_atv.gif','../../img/btn_produtos_atv.gif','../../img/btn_contato_atv.gif')">

<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">

  <tr>

    <td align="center" valign="top"><table width="666" border="0" cellspacing="0" cellpadding="0">

      <tr>

        <td align="center" valign="top"><table width="666" border="0" cellspacing="0" cellpadding="0">

            <tr>

              <td width="374" rowspan="2" align="left" valign="bottom"><a href="../home/index.php"><img src="../../img/logo_manivela.gif" width="249" height="114" border="0" /></a></td>

              <td width="292" height="65" align="right" valign="middle">&nbsp;</td>

            </tr>

            <tr>

              <td align="right" valign="bottom"><table width="10%" border="0" cellspacing="0" cellpadding="0">

                <tr>

                  <td><a href="http://www.manivela.com.br/site/content/home" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('home','','../../img/btn_home_atv.gif',1)"><img src="../../img/btn_home.gif" name="home" width="67" height="22" border="0" id="home" /></a></td>

                  <td><a href="http://www.manivela.com.br/site/content/produtos" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('produtos','','../../img/btn_produtos_atv.gif',1)"><img src="../../img/btn_produtos.gif" name="produtos" width="66" height="22" border="0" id="produtos" /></a></td>

                  <td><a href="http://www.manivela.com.br/site/content/contato" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('contato','','../../img/btn_contato_atv.gif',1)"><img src="../../img/btn_contato.gif" name="contato" width="66" height="22" border="0" id="contato" /></a></td>

                </tr>

              </table></td>

            </tr>

          </table></td>

      </tr>

      <tr>

        <td align="center" valign="top" background="../../img/bgr_fundo_centro.gif"><table width="666" border="0" cellpadding="0" cellspacing="0">

         

          <tr>

            <td align="center" valign="bottom"><img src="../../img/topo_vermelho.gif" width="666" height="14" /></td>

          </tr>

          <tr>

            <td align="center" valign="top" background="../../img/bgr_fundo_centro.gif"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="666" height="174">

              <param name="movie" value="../../media/topo.swf" />

              <param name="quality" value="high" />

    <param name="menu" value="false" />

              <embed src="../../media/topo.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="666" height="174" menu="false"></embed>

            </object></td>

          </tr>

          <tr>

            <td align="center" valign="top"><table width="664" height="66" border="0" cellpadding="0" cellspacing="0">

              <tr>

                <td align="center" valign="top" background="../../img/bg_menu.gif"><table width="99%" border="0" cellspacing="0" cellpadding="0">

                  <tr>

                    <td height="24">&nbsp;</td>

                  </tr>

                  <tr>

                    <td height="23" align="left" class="menu"><a href="../empresa/index.php">Empresa</a>&nbsp;|&nbsp;<a href="../produtos/index.php">Produtos</a> | <a href="../dicas/index.php">Dicas</a> | <a href="../roteiros/index.php">Roteiros</a>&nbsp;|&nbsp;<a href="../eventos/index.php">Eventos</a>&nbsp;| <a href="../como_comprar/index.php">Como Comprar</a>  | <a href="../confirmacao_compra/index.php">Confirma&ccedil;&atilde;o de Compra</a> | <a href="index.php">Cadastre-se</a> |&nbsp;<a href="../contato/index.php">Contato</a> </td>

                  </tr>

                </table></td>

              </tr>

            </table></td>

          </tr>

         

        </table>

          <!-- InstanceBeginEditable name="center" -->

   

   

   

   

   

    <script language=javascript>

function obterCep()

{

if (!validarCampo(document.frm.cep, 'cep')) return;

window.document.getElementById("msgBuscaCep").style.left = 350;

window.document.getElementById("msgBuscaCep").style.top = 480;

window.document.getElementById("msgBuscaCep").style.visibility = 'visible';

document.frm.action="cadastro_cnpj3.php";

document.frm.submit();

}

function enviar()

{

if (!validarCampo(document.frm.nome,'nome')) return;

if (document.frm.tipoPessoa[0].checked)

{

  if (!validarCampo(document.frm.cpf, 'cpf')) return;

  if (!validarCPF(document.frm.cpf.value))

  {

  window.alert('Cpf inválido!');

  document.frm.cpf.focus();

  return;

  }

}

else

{

  if (!validarCampo(document.frm.cnpj, 'cnpj')) return;

  if (!validarCNPJ(document.frm.cnpj.value))

  {

  window.alert('CNPJ inválido!');

  document.frm.cnpj.focus();

  return;

  }

}

if (!validarCampo(document.frm.cep, 'cep')) return;

if (!validarCampo(document.frm.endereco, 'Logradouro')) return;

if (!validarCampo(document.frm.numero, 'numero')) return;

if (document.frm.termo.checked)

{

  document.frm.action="cadastro_cnpj3.php";

  document.frm.submit();

}

else

{

  window.alert('É necessário ler e concordar como o \"Termo de Acordo\" para prosseguir');

  document.frm.termo.focus();

}

}

function alterarCampo(valor)

{

if (valor == 'F')

{

  document.getElementById("textoCpfCnpj").innerHTML = 'CPF:';

  objeto = "<input class=\"frm\" type=\"text\" name=\"cpf\"" ;

  objeto += "onkeypress = \"javascript:return formatarDados(this, (navigator.appName == 'Netscape')?event.which:event.keyCode, '000.000.000-00')\" ";

  objeto += "maxlength=\"14\" id=\"lab3\" value=\"\" />"

  document.getElementById("inputCpfCnpj").innerHTML = objeto;

 

}

else

{

  document.getElementById("textoCpfCnpj").innerHTML = 'CNPJ:';

  objeto = "<input class=\"frm\" type=\"text\" name=\"cnpj\"" ;

  objeto += "onkeypress = \"javascript:return formatarDados(this, (navigator.appName == 'Netscape')?event.which:event.keyCode, '00.000.000/0000-00')\" ";

  objeto += "maxlength=\"18\" id=\"lab3\" value=\"\" />"

  document.getElementById("inputCpfCnpj").innerHTML = objeto;

}

}

function validarCampo(objeto, nome)

{

if (objeto.value == "" || objeto.value == null)

{

  window.alert('Campo ' + nome + ' inválido');

  objeto.focus();

  return false;

}

return true;

}

/* formata mascara de dados */

function formatarDados(obj,codigo,mascara){

    var num = "0123456789";

var backspace = 8;

var tab = 0;

var caracteres = num;

if(codigo == backspace || codigo == tab ){

  return true;

}

    if(obj.value.length == mascara.length) {

  return false;

    }

    var pos = obj.value.length;

  if (mascara.charAt(pos) != '0'){

      obj.value = obj.value + mascara.charAt(pos);

      pos++;

  }

    if(mascara.charAt(obj.value.length) == '0') {

    if(caracteres.indexOf(String.fromCharCode(codigo)) >= 0) {

  return true;

  }

}

return false;

}

/* retira mascara de dados */

function desformatarDados(obj){

    if ((obj.value == "") || (obj.value == null)){

        return "";

    }

    resposta = "";

    for (i = 0; i < obj.value.length; i++) {

        if ((obj.value.charAt(i) != ".") && (obj.value.charAt(i) != ",")

  && (obj.value.charAt(i) != "-") && (obj.value.charAt(i) != "/")

            && (obj.value.charAt(i) != ":") && (obj.value.charAt(i) != "(")

            && (obj.value.charAt(i) != ")"))

        {

            resposta = resposta + obj.value.charAt(i);

        }

    }

    return resposta;

}

function aceitarCaracteresValidos(codigo,tValor){

    alfa = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    alfaM = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

caracEspecial = "[email protected]#$%¨&*()_-+=§'?/\|;:.}{[]<>";

caracEspecialSemAspas = "[email protected]#$%&*()_-+=§?/\|;:.}{[]<>";

    numerico = "1234567890";

arroba = "@";

espaco = " ";

virgula = ",";

ponto = ".";

barra = "/";

traco = "-";

underScore = "_";

acentos = "ÀÈÌÒÙàèìòùÁÉÍÓÚáéíóúÄËÏÖÜäëïöüÂÊÎÔÛâêîôûÃÕãõÑñÇç";

    parenteses = "()";

    aspas = '"'

caracValido = "";

backspace = 8;

tab = 0;

if(codigo == backspace || codigo == tab ){

  return true;

}

switch(tValor){

  case 1:{ //nome

  caracValido = alfa + numerico + acentos + espaco;

  break;

  }

  case 2:{ //numero

  caracValido = numerico;

  break;

  }

  case 3:{ //email

  caracValido = alfa + numerico + arroba + traco + underScore + ponto;

  break;

  }

}

if(caracValido.indexOf(String.fromCharCode(codigo)) >= 0 || codigo == 13)

  return true;

else

  return false;

}

function validarCPF(numeroCPF){

valor = numeroCPF;

if(numeroCPF.length != 11 && numeroCPF.length != 14){

  return false;

}

if (numeroCPF.length > 11){

  if (numeroCPF.indexOf(".") < 0 && numeroCPF.indexOf("-") < 0){

  numeroCPF = numeroCPF.substring(3,14);

  }

  CPF = "";

  for(i=0;i<(numeroCPF.length);i++){

  if (numeroCPF.charAt(i) != "." && numeroCPF.charAt(i) != "-"){

    CPF += numeroCPF.charAt(i);

  }

  }

}

else{

  CPF = numeroCPF;

}

if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" ||

  CPF == "22222222222" || CPF == "33333333333" || CPF == "44444444444" ||

  CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||

  CPF == "88888888888" || CPF == "99999999999"){

  return false;

}

soma = 0;

for (i=0; i < 9; i ++)

  soma += parseInt(CPF.charAt(i)) * (10 - i);

resto = 11 - (soma % 11);

if (resto == 10 || resto == 11)

  resto = 0;

if (resto != parseInt(CPF.charAt(9))){

  return false;

}

soma = 0;

for (i = 0; i < 10; i ++)

  soma += parseInt(CPF.charAt(i)) * (11 - i);

resto = 11 - (soma % 11);

if (resto == 10 || resto == 11)

  resto = 0;

if (resto != parseInt(CPF.charAt(10))){

  return false;

}

return true;

}

function validarCNPJ(numeroCNPJ){

if (numeroCNPJ.length != 14 && numeroCNPJ.length != 18){

  return false;

}

if (numeroCNPJ.length > 14){

  CNPJ = "";

  for(i=0;i<numeroCNPJ.length;i++){

  if (numeroCNPJ.charAt(i) != "." && numeroCNPJ.charAt(i) != "-" && numeroCNPJ.charAt(i) != "/"){

    CNPJ = CNPJ + numeroCNPJ.charAt(i);

  }

  }

}

else{

  CNPJ = numeroCNPJ;

}

// Recebe o CNPJ e informa se

// é falso ou verdadeiro ' O sinal > indica que a linha continua

var numCNPJ, DV;

var Soma, Digito;

var i, j;

var Controle, Mult;

// Identifica as 2 partes do CNPJ

numCNPJ = CNPJ.substring(0,12);

DV = CNPJ.substring(12);

// Multiplicadores que fazem parte

// do algoritmo de checagem

Mult = "543298765432";

// Inicializa a variável Controle

Controle = "";

// Loop de verificação

for(j=0; j < 2; j++){

  Soma = 0;

  for(i=0; i < 12; i++){

    Soma = Soma + numCNPJ.charAt(i) * Mult.charAt(i);

  }

  if(j == 1){

  Soma = Soma + (2 * Digito);

  }

  Digito = (Soma * 10) % 11 ;

  if(Digito == 10) Digito = 0;

  Controle = Controle + Digito;

  // Seqüência de multiplicadores para

  // o cálculo do segundo Digito

  Mult = "654329876543";

}

// Compara dígitos calculados (Controle)

// com dígitos informados (DV)

if(Controle != DV){

  return false;

}

else{

  return true;

}

}

</SCRIPT>

    <table width="645" border="0" cellspacing="0" cellpadding="0">

            <tr>

              <td><img src="img/tit_cadastro.gif" width="624" height="20" /></td>

            </tr>

            <tr>

              <td><br />

                <span class="bold66">Cadastro de Pessoa Jur&iacute;dica. </span></td>

            </tr>

            <tr>

              <td>&nbsp;</td>

            </tr>

            <tr>

              <td><span class="formlogin2" style="MARGIN: 0px 0px 10px">

                <input id="lab1"

                        onclick="alterarCampo(this.value)" type="radio" value="F"

                        name="tipoPessoa" />

                <label class="txtAzulnormal"

                        style="MARGIN-RIGHT: 20px" for="lab1">Pessoa

                        f&iacute;sica</label>

                <input id="lab1"

                        onclick="alterarCampo(this.value)" type="radio" value="J"

                        name="tipoPessoa" />

                <label class="txtAzulnormal"

                        for="lab1">Pessoa jur&iacute;dica</label>

              </span></td>

            </tr>

            <tr>

              <td><table width="520" border="0" align="left" cellpadding="3" cellspacing="3" class="formlogin2">

                <tr>

                  <td width="140" align="left" valign="top">Nome:</td>

                  <td width="357" align="left" valign="top"><label>

                    <input name="textfield" type="text" class="formlogin" size="40" maxlength="60" />

                  </label></td>

                </tr>

                <tr>

                  <td width="140" align="left" valign="top">CPF:</td>

                  <td align="left" valign="top"><label>

                    <input name="textfield2" type="text" class="formlogin" size="25" maxlength="14" />

                  </label></td>

                </tr>

                <tr>

                  <td width="140" align="left" valign="top">E-mail:</td>

                  <td align="left" valign="top"><label>

                    <input name="textfield3" type="text" class="formlogin" size="40" maxlength="60" />

                  </label></td>

                </tr>

                <tr>

                  <td width="140" align="left" valign="top">Telefone Residencial: </td>

                  <td align="left" valign="top"><label>

                    <input name="textfield4" type="text" class="formlogin" size="25" maxlength="25" />

                  </label></td>

                </tr>

                <tr>

                  <td width="140" align="left" valign="top">Telefone Comercial: </td>

                  <td align="left" valign="top"><label>

                    <input name="textfield5" type="text" class="formlogin" size="25" maxlength="25" />

                  </label></td>

                </tr>

                <tr>

                  <td width="140" align="left" valign="top">Telefone Celular: </td>

                  <td align="left" valign="top"><label>

                    <input name="textfield6" type="text" class="formlogin" size="25" maxlength="25" />

                  </label></td>

                </tr>

                <tr>

                  <td align="left" valign="top">CEP:</td>

                  <td align="left" valign="top"><form action="cadastro_cnpj3.php" method="post" name="form1" target="_self" id="form1" valign="top">

                      <div align="left">

                        <input name="CEP" type="text" class="formlogin" id="CEP3" size="11" maxlength="10"onblur="javascript: obterCep();" onkeypress="javascript:return formatarDados(this, (navigator.appName == 'Netscape')?event.which:event.keyCode, '00000-000')" />

                        <input name="Submit" type="submit" class="formlogin" value="Enviar" />

                        </div>

                  </form></td>

                </tr>

                <tr>

                  <td colspan="2" align="left" valign="top"><?php

$conecta = mysql_connect("localhost", "login", "senha") or print (mysql_error());

mysql_select_db(dvmark_manivela, $conecta) or print(mysql_error());

$qr = "SELECT * FROM CEP where CEP = '$CEP'";

$exe = mysql_query($qr, $conecta) or print(mysql_error());

echo "<table width='520' valign='top' height='106' border='0' align='left' cellpadding='3' cellspacing='3' class='formlogin2'>";

echo "<tr>";

while ($linha=mysql_fetch_array($exe)) {

$ENDERECO = $linha["ENDERECO];

$CEP = $linha["CEP"];

$CIDADE = $linha["CIDADE"];

$BAIRRO = $linha["BAIRRO"];

$UF = $linha["UF"];

echo "<td width='100'>Endere&ccedil;o:</td>";

echo "<td width='300'><input name='textfield2'  width='100' align='left' valign='top' type='text' class='formlogin' size='50' maxlength='50' value='$ENDERECO'></td>";

echo "</tr>";

echo "<tr>";

echo "<td>Cidade:</td>";

echo "<td><input name='textfield22' type='text' width='100' align='left' valign='top' class='formlogin' size='50' maxlength='50' value='$CIDADE'></td>";

echo "</tr>";

echo "<tr>";

echo "<td>Bairro:</td>";

echo "<td><input name='textfield23' type='text' width='100' align='left' valign='top' class='formlogin' size='30' maxlength='50' value='$BAIRRO'></td>";

echo "</tr>";

echo "<tr>";

echo "<td>UF:</td>";

echo "<td><input name='textfield24' type='text' width='100'  align='left' valign='top' class='formlogin' size='30' maxlength='10' value='$UF'></td>";

}

echo "</tr>";

echo "</table>";

?></td>

                  </tr>

          </table></td>

            </tr>

            <tr>

              <td><script src="../../js/default.js">

                    </script>

              <br />

   

<br />

</form>              </td>

            </tr>

            <tr>

              <td>&nbsp;</td>

            </tr>

          </table>

          <!-- InstanceEndEditable --><br />

          <img src="../../img/bgr_down.gif" width="679" height="33" border="0" usemap="#Map" /></td>

      </tr>

    </table></td>

  </tr>

</table>

<map name="Map" id="Map"><area shape="rect" coords="478,7,529,21" href="http://www.dvmark.com.br" target="_blank" />

</map></body>

<!-- InstanceEnd --></html>

Share this post


Link to post
Share on other sites

1 answer to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this