How to generate value by clicking on an input type Submit

Asked

Viewed 1,185 times

3

Form to send selected data for deletion.

<form action="index.php?pagina=../controller/controllerUser&acao=ok" method="POST" enctype="multipart/form-data">           
                        <label>Nome:</label>
                        <input class="input-xlarge" type="text" value="<?php echo $nome; ?>" name="nome" autofocus=""> <br/>

                        <label>CPF:</label>
                        <input class="input-large" type="text" name="cpf"  autofocus=""> <br/>

                        <label>Senha:</label>
                        <input class="input-large" type="text" name="senha"  autofocus=""> <br/>

                        <label>Repetir a senha:</label>
                        <input class="input-large" type="text" name="confirmarSenha"  autofocus=""> <br/>

                        <label>CEP:</label>
                        <input class="input-medium" type="text" name="cep"  autofocus=""> <br/>

                        <label>Endereço</label>
                        <input class="input-xlarge" type="text" name="descricao"  autofocus=""> <br/>

                        <label>Complemento</label>
                        <input class="input-xlarge" type="text" name="complemento" autofocus=""> <br/>


                </div>
                <div class="span6">

                    <label>E-mail</label>
                        <input class="input-xlarge" type="text" name="email" autofocus=""> <br/>

                    <label>Telefone</label>
                    <input class="input-xlarge" type="text" name="telefone"  autofocus=""> <br/>

                    <label>Telefone Dois</label>
                    <input class="input-xlarge" type="text" name="telefoneDois"  autofocus=""> <br/>    

                    <label>Selecione um País</label>
                    <select required="" name="id_pais">
                        <?php
                        $daoPais->selecionaTodosPaises();
                        ?>
                    </select>


                    <label>Selecione um estado</label>    
                    <select required="" name="idEstado" id="idEstado" onchange="getValorEstado(this.value, 0)">
                        <option selected="" disabled="" value="nulo">Selecione o ESTADO</option>
                        <?php
                        $daoEstado->selecionaTodosEstados();
                        ?>
                    </select>

                    <label>Selecione uma Cidade</label>    
                    <select required="" id="idCidade" name="idCidade">
                        <option selected="" disabled="" value="nulo">Selecione o ESTADO ANTES</option>
                        <!-- chadamada de arquivo aqui com jquery utilizando
                             ajax, o arquivo se encontra uteis/selectCidade.php-->
                    </select>
                    <br></br>


                    <input class="btn btn-large btn-primary" type="submit" value="salvar">
                    <a class="btn btn-large btn-danger" href="index.php?pagina=PaginaResumo" value="Cancelar">Cancelar</a>
                    </form>

will redirect to the controller page you insert or delete so far.

if (isset($_GET["acao"]) and $_GET["acao"] == "ok") {

//primeiro inserir o endereço
//atributos de endereço
$descricao = $_POST["descricao"];
$complemento = $_POST["complemento"];
$cep = $_POST["cep"];
$idCidade = $_POST["idCidade"];


//objeto de endereco
$endereco = new EnderecoEntity("", $descricao, $complemento, $cep, $idCidade);

//dao de endereço
$daoEndereco = new EnderecoDao();
$idEndereco = $daoEndereco->inserirEndereco($endereco);

//depois inserir o usuário com id de endereço

$nome = $_POST["nome"];
$cpf = $_POST["cpf"];
$senha = $_POST["senha"];

$usuario = new UsuarioEntity("", $nome, $cpf, $senha, $idEndereco);

$usuarioDao = new UsuarioDao();
$idUsuario = $usuarioDao->inserirUsuario($usuario);


//depois inserir o telefone com id de usuário
$telefoneUm = $_POST["telefone"];
$telefoneDois = $_POST["telefoneDois"];

$telefone = new TelefoneEntity("", $telefoneUm, $telefoneDois, $idUsuario);
$telefoneDao = new TelefoneDao();
$telefoneDao->inserirTelefone($telefone);


//depois inserir email com id de usuário
$email = $_POST["email"];
$email = new EmailEntity("", $email, $idUsuario);
$emailDao = new EmailDao();
$emailDao->inserirEmail($email);
 }
if (isset($_GET["acao"]) and $_GET["acao"] == "deletar") {

$idEndereco = $_GET["id_endereco"];
$idPessoa = $_GET["id"];
//verificação de confirmação do usuário
?>   
<div class="alert alert-error">
    //o ponto de erro é esse sei que vou perder os dados quando tentar retornar para esse form
    <form action="index.php?pagina=../controller/controllerUser&acao=deletarConcluido"> method="POST" enctype="multipart/form-data">
        <h4>Atenção!</h4>
        Você realmente deseja excluir o usuário: 
        <?php
        echo $_GET["nome"];
        echo'<br/>';
        echo ' <a class="btn btn-primary" href="index.php?pagina=PaginaResumo">Não</a>';
        echo ' <input type="submit" class="btn btn-danger" value="SIM">';
    ?>  
    </form>
</div>
<?php
 if(isset($_GET["acao"]) and $_GET["acao"] == "deletarConcluido") {

    $idEndereco = $_GET["id_endereco"];
    $idPessoa = $_GET["id"];

    //Deletar email
    $emailDao = new EmailDao();
    $emailDao->deleteEmail($idPessoa);

    //deletar telefone
    $telefoneDao = new TelefoneDao();
    $telefoneDao->deleteTelefone($idPessoa);

    //Deletar usuario
    $usuarioDao = new UsuarioDao();
    $usuarioDao->deletePessoa($idPessoa);

    //Deletar endereço
    $enderecoDao = new EnderecoDao();
    $enderecoDao->deleteEndereco($idEndereco);
} 
}
  • Puts an Hidden field in the form.

  • the problem is that are two different Forms, ie in one I have a table, and in the other only two buttons, if I pass a variable in this other it loses the previous values, is the mix of php without framework seems servelet.

  • I confess that I didn’t understand it very well, but why would I lose value if I added an Hidden field? It would just be added. You can only run one form at a time, so if you add Hidden to the form that will run, I see no problem. But edit the question and put these forms so I can understand better.

  • 2

    I think the problem is logic, not form. Describe the case better.

  • Make a delete confirmation for a job more suitable for php javascript, post the code of the two Forms.

  • the problem is that I would have to submit the other form, I believe that generating a variable in javascript and passing it could work

  • testei puts it in the controller form like this but it didn’t work <form action="index.php? page=.. /controller/controllerUser&acao=deletarConclude & idEndereco='. <? php echo $idEndereco ? >. '&idPessoa='. <? php echo $idPessoa? >. '" method="POST" enctype="Multipart/form-data">

  • I just did not understand why you do not create the boot delete inside this form? Ai instead of you use the get

  • @Andrei Coelho the functionality of delete is ok, what I need to do is make a confirmation in this form for really the user who wants to delete do this or simply give up and do not do this.

  • Got it. I think javascript solves it. I put the javascript tag in the tags to see if a professional appears in this language

  • @rray I just posted the code, some suggestion?

  • Is this form to create a record and delete? it will be used to make tbm changes?

Show 7 more comments

2 answers

1


You can do it that way:

<div class="alert alert-error">
        <h4>Atenção!</h4>
        Você realmente deseja excluir o usuário: 
        <?php
        echo $_GET["nome"];
        echo'<br/>';
        ?>

        <a class="btn btn-primary" href="index.php?pagina=PaginaResumo">Não</a>
        <a class="btn btn-primary" href="index.php?pagina=../controller/controllerUser&acao=deletarConcluido&id_endereco=<?php echo $id_endereco; ?>&id=<?php echo $id_pessoa; ?>">Sim</a>


</div>

Post the result and we’ll get better if you need.

Updating:

This way you don’t need to generate that form in php, the page won’t even need to be reloaded to generate the confirmation:

<script>
function confirmarDelete(delUrl) {
  if (confirm("Você realmente deseja excluir o usuário?")) {
    document.location = delUrl;
  }
}
</script>

<a href="javascript:confirmarDelete('index.php?pagina=../controller/controllerUser&acao=deletarConcluido&id_endereco=<?php echo $id_endereco; ?>&id=<?php echo $id_pessoa; ?>')">Deletar</a>

Remembering that this is a basic way of doing, there are more advanced and more personalized ways, but this will give you a good basis to start.

  • yes had already tried this way, passes the parameters quietly again, even had put in a previous comment, now do not know why it does not enter the check if there is the variable if(isset($_GET['acao'])) { do something.... even showing the action in the url it is not entering this condition.

0

@Diego Machado everything happened well I only put in the form the variables because the system is almost all that way so it was as follows:

<div class="alert alert-error">
    <form action="index.php?pagina=../controller/controllerUser&novaAcao=deletarConcluido&id_endereco=<?php echo $idEndereco; ?>&id=<?php echo $idPessoa;?>" method="POST" enctype="multipart/form-data">
        <h4>Atenção!</h4>
        Você realmente deseja excluir o usuário: 
        <?php
            echo $_GET["nome"];
            echo'<br/>';
            echo ' <a class="btn btn-primary" href="index.php?pagina=PaginaResumo">Não</a>';
            echo '<input type="submit" value="Sim" class="btn btn-primary" >';
        ?>        
    </form>

Doubts that remained: 1) Is this the right way to do it? 2) this form is correct?

  • I’ll be honest, I don’t think this is an efficient way. It complicates your code. You probably have a list with buttons to delete, which when clicked lead to that page, right? All the data should be on the link, as I showed. The confirmation should come through a javascript, see this simple example: http://www.w3schools.com/js/tryit.asp?filename=tryjs_confirm This way the page does not need to be loaded, and you do not need to create a form to confirm. You will just redirect. I will assemble an example later for you.

Browser other questions tagged

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