How to redirect the ID of a current registration to print

Asked

Viewed 283 times

0

I want to redirect an id that I will get only after clicking register, at this time I perform the insertion in the database in php and soon after I want to redirect via javascript to a page printing data that I have, my problem is that I do not have the data of this id, because I just registered the user and I don’t have the current ID in any variable, how can I get the current dial I registered? When inserting the var line f = "" in javascript simply stops working which may be wrong ?

Redirect (javascript):

    function dec(){
  var f = "<?php echo $ultimoID;?>"
decisao = confirm("Deseja imprimir a ficha do cliente cadastrado?");
if (decisao)
window.open('imprimir_clientes.php?id='+f, '_blank');
}

register database (php):

              <?php if (isset($_GET['cadastra']) && $_GET['cadastra'] == 'add') {
          $nascimento = implode("-", array_reverse(explode("/",$_GET['nascimento'])));
            $cadastra = mysql_query("INSERT INTO t_cadclientes (Snome, Endereco,DataNascimento, Bairro, Cidade, Uf, Fone, Cpf, Cgc, Identidade, Pai, Mae,
                                     EstadoCivil, Cep, NomeEmpresa, EndEmpresa, BairroEmpresa, CidadeEmpresa, FoneEmpresa, Ramal, Renda,
                                     NomeConjuge,EmpresaConjuge,EndEmpresaConjuge,BairroEmpresaConjuge,CidadeEmpresaConjuge,FoneEmpresaConjuge,
                                     RamalEmpresaConjuge,RendaConjuge, NomeContato, EndContato, FoneContato,Relacao, Loja1, 
                                     Loja2, FichaDesde) 
                                    VALUES (UPPER('$_GET[nome]'), UPPER('$_GET[endereco]'), '$nascimento', UPPER('$_GET[bairro]'), UPPER('$_GET[cidade]'), '$_GET[uf]', '$_GET[telefone]',
                                     '$_GET[cpf]', '$_GET[cp]', '$_GET[identidade]', UPPER('$_GET[nomepai]'), UPPER('$_GET[nomemae]'), '$_GET[estadocivil]', '$_GET[cep]',
                                     UPPER('$_GET[nomeempresa]'), UPPER('$_GET[enderecoempresa]'), UPPER('$_GET[bairroempresa]'), UPPER('$_GET[cidadeempresa]'), '$_GET[telefoneempresa]',
                                     '$_GET[ramalempresa]', '$_GET[renda]', UPPER('$_GET[nomeconjuge]'), UPPER('$_GET[empresaconjuge]'), UPPER('$_GET[enderecoempresaconjuge]'),
                                     UPPER('$_GET[bairroempresaconjuge]'), UPPER('$_GET[cidadeempresaconjuge]'), '$_GET[foneempresaconjuge]', '$_GET[ramalconjuge]', '$_GET[rendaconjuge]',
                                     UPPER('$_GET[nomecontato]'), UPPER('$_GET[enderecocontato]'), '$_GET[telefonecontato]', UPPER('$_GET[relacaocontato]'), UPPER('$_GET[referenciacontato1]'),
                                     UPPER('$_GET[referenciacontato2]'), '$_GET[dataficha]')");
$ultimoID= mysql_insert_id();
    if($cadastra == '1') {
       // Montamos o caminho para o mesmo script:
       $url = 'http://'.$_SERVER["SERVER_NAME"].'/'.$_SERVER["PHP_SELF"];
       // Deixamos a mensagem para depois do redirect:
       $mensagem = urlencode( 'Cadastro realizado com sucesso' );
       // Redirecionamos repassando a mensagem adiante:
       header( "Location: $url?mensagem=$mensagem" );
       // E encerramos o script neste ponto:
       die();
    }else{
       echo "Erro ao cadastrar !";
    }
    }
    if ( isset( $_GET['mensagem'] ) ) {
       echo htmlentities($_GET['mensagem']);
        echo"<script>";
          echo"javascript: dec();";
        echo "</script>";
    }
    ?>
  • Please explain the changes you have made to the question code. It is important not to invalidate the answers you have already given. If your changes are a feedback to Eduardo’s response, make a comment warning him. . . . . [ps] Indenting code helps much in reading and troubleshooting.

  • Hello @brasofilo thanks! already edited the question, you can help me?

  • @Eduardooliveira thank you very much, I edited my question by inserting the $ultimoID= mysql_insert_id(); after the Insert in the bank, but when I enter the var f - $ultimoID; the javascript stops working, do you have any idea what it might be? abs

1 answer

3

You can use mysql_insert_id();

It takes the last registered ID

$Codigo = mysql_insert_id();

From what I saw in your code, after giving the INSERT, you are redirected to another page... in this case you can pass via URL same

Location: $url?mensagem=$mensagem&id=$Codigo

Or fetch the last table ID from MYSQL:

SELECT MAX ('id') FROM 't_cadclientes'; //nesse caso estou chamando seu campo de 'id', basta colocar o nome do campo da sua tabela.

These are simpler steps, but you could create a Session for example..

$_SESSION['Codigo'] = $Codigo;

But to work with Sessions you need to give the session_start().

  • I edited my question by entering the $ultimoID= mysql_insert_id(); after the Insert in the bank, but when I enter the var f - $ultimoID; the javascript stops working, do you have any idea what it might be? abs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.