Button does not connect with Javascript to scan

Asked

Viewed 46 times

0

Below are the code of the two pages I am using, the "btnCadastrarUsuario" button should call the js, but when it is clicked appears a blank page written "false".

registersComponent.class.php

inserir a descrição da imagem aqui

class CadastroUsuarioComponent implements IComponents {

 public static function ajax(){
    if(isset($_REQUEST['acaoAjax']) and !isset($_POST['btnCadastrarUsuario'])){
        $method = isset($_REQUEST['acaoAjax'])?$_REQUEST['acaoAjax']:NULL;
        $cadastroUsuarioComponent = new CadastroUsuarioComponent();
        $cadastroUsuarioComponent->show($method);
    }
}
// Fim método responsável pelas solicitações Ajax
public function show($method) {
    switch ($method){
        case 'indexController':
            $this->indexController();
            break;          
        case 'validarCpfCadastrado':
            $this->validarCpfCadastrado();
            break;
        case 'cadastrarUsuario':
            $this->cadastrarUsuario();
            break;
        case 'posCadastroUsuario':
            $this->posCadastroUsuario();


        default:
            break;
    }    
}

public function indexController() {
    echo '<div id="inserir">
    <form method="post" name="frmCadastroUsuario" id="frmCadastroUsuario">
    <input type="hidden" name="acaoAjax" value="validarCpfCadastrado" />
    <input type="hidden" name="component" value="CadastroUsuarioComponent" />
    <input type="hidden" name="method" value="cadastrarUsuario" />

    <h1>CADASTRO</h1><br/>

        <input type="text" class="insira" placeholder="Insira seu nome completo..." name="nome" id="nome" />

        <input type="text" class="insira" placeholder="Insira seu CPF..." name="cpf" id="cpf" />

        <input type="text" class="insira" placeholder="Insira seu email..." name="email" id="email" />

        <input type="password" class="insira" placeholder="Insira sua senha..." name="senha" id="senha" />

        <input type="password" class="insira" placeholder="Confirme sua senha..." name="confsenha" id="confsenha"/>

        <!--<button type="button" name="btn_cadastro" id="btn_cadastro">Cadastrar</button>-->
        <input type="submit" value="Cadastrar" name="btnCadastrarUsuario" />

    </form>
</div>
<div id="resultado"></div>';

echo '<script src="js/jsCadastroUsuarioComponent.js" type="text/javascript"></script>';     
}

public function validarCpfCadastrado(){
    //Recebe os elementos via GET
    $cpf = $_REQUEST['cpf']; 

    $usuario = new Usuario();
    $arrayUsuario = $usuario->select("and cpf_usu='{$cpf}'");

    //Aqui voc� verifica em seu banco de dados, se o login j� foi cadastrado.
    //Como exemplo vou verificar se o valor digitado está no array
   // if (in_array($email, $logins_cadastrados)){
    if(count($arrayUsuario)> 0){
    //Se o login já existir você exibe false
        echo 'false';
    }else if(!preg_match('/^[0-9]{3}\.[0-9]{3}\.[0-9]{3}\-[0-9]{2}$/',$cpf)){
        echo 'false';
    }else{
    //Se o login não existir você exibe true
        echo 'true';
    }    
    exit();
}

public function cadastrarUsuario(){

    $tipoUsuario =  new TipoUsuario();
    $formacao = new Formacao();
    $arrayTipoUsuario = $tipoUsuario->select("and nome_tipo_usu='SDE'");
    $arrayFormacao = $formacao->select("and nome_forma='PUBLI'");
    $usuario = new Usuario();
    $usuario->setNomeUsu($_REQUEST['nome']);
    $usuario->setCpfUsu($_REQUEST['cpf']);
    $usuario->setEmailUsu($_REQUEST['email']);
    $usuario->setSenhaUsu(Bcrypt::hash($_REQUEST['senha']));
    $usuario->setCodForma($arrayFormacao[0]['cod_forma']);
    $usuario->setCodTipoUsu($arrayTipoUsuario[0]['cod_tipo_usu']);
    $usuario->setCodStatusUsu('I');

    if($usuario->insert()->rowCount() > 0){
        $msg = ' Cadastro efetuado, aguarde o contato do Administrador.';
        //echo 'Você está cadastrado, aguarde o contato do administrador!';
    }else{
        $msg = ' Não foi possível efetuar seu cadastro, retorne mais tarde!';
       // echo 'Não foi possível efetuar seu cadastro, retorne mais tarde!';
    } 
   // $this->posCadastroUsuario($msg);
    $_SESSION['msg'] = $msg;
    echo '<script>';
    echo "location.href='?component=CadastroUsuarioComponent&method=posCadastroUsuario'";
    echo '</script>';

} inserir a descrição da imagem aqui

jsCadastroUsuarioComponent.js inserir a descrição da imagem aqui

By clicking the button, without writing data in the fields, the code should do this: inserir a descrição da imagem aqui

Thanks in advance!

  • 1

    Welcome to Stack Overflow :D Visit the [tour] to learn how the site works. I recommend putting the code WRITTEN, not images of it.

1 answer

1

You have a form, and a button like Ubmit inside that form. This is an HTML feature, with this layout, when you click on the button you will send the form data to the server, and a new page with the answer will open.

If you don’t want this to happen, remove Ubmit. On the other hand, you can also leave Submit, and put an event on your button via Javascript by calling the Event.preventDefault() method so as not to send the information by clicking it, this way your website will be functional even if the user decides to disable Javascript.

  • Hello, thank you for the answer! If I remove Ubmit, why should I change it? I don’t know much about Javascript and I used my PHP teacher’s model as a reference, he uses Submit and the page works as it should, I asked another teacher today and he said that there is an "activator" missing somewhere in the code, but as he was of another subject could not help me with this, if I change the Submit he will still send data to the register?

  • You don’t have to replace it with anything, just erase the guy. After the button validate the fields you can use the seuForm.Submit() method to send the data. If you don’t want to open a new page, you have to use AJAX.

  • When I remove the Submit the button becomes a text field

  • Okay, use like button then.

  • No longer presents the "false", but also does not show the check and does not register

Browser other questions tagged

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