jQuery check field php database

1

I have the following button:

<button type="submit" class="btn btn-info pull-right"><? echo $titulo; ?></button>

This button sends a form action, with the data filled in.

And I also have the following field:

<input type="text" class="form-control" name="con_email" id="con_email" placeholder="E-mail da Consultora" value="<? if(isset($dados->con_email)){ echo $dados->con_email; } ?>" required>

I need to, by pressing the submit button, check if this email already exists in the database. I know it's possible to work this out in jQuery, however, I do not know where to start checking.

I just thought about sending, I search using php and return true or false, returning, true, I just call a modal, returning false, I call ajax to insert into the database, using the get or post method .

Does anyone have any idea how I can craft this jQuery?

    
asked by anonymous 16.08.2016 / 14:50

2 answers

3

I imagine your html looks like this or something:

<form action="gravar.php" method="post" id="formGravar">

    <input type="text" class="form-control" name="con_email" id="con_email" placeholder="E-mail da Consultora" value="<? if(isset($dados->con_email)){ echo $dados->con_email; } ?>" required>
    <button type="button" id="btnCheck" class="btn btn-info pull-right"><? echo $titulo; ?></button>

</form>

The javascript you can do as follows:

<script>
$("#btnCheck").on('click', function(){
    $.post('checar_email.php', {email: $("#con_email").val()}, function(r){
        if(r == '0'){
            $("#formGravar").submit();
        }else{
            alert('e-mail já existe na base');
        }
    });
});
</script>

The javascript will act on the button without working with the form initially. It will send an ajax to PHP "checar_email.php" where you should receive the $ _POST ['email'] and do the check in the base whether or not you have the email entered. If you have "1" return and it shows an alert and if not "0" will give a submit in the form that sends to the "record.php" that will insert the email in the database.

"checar_email.php" file looks something like this:

<?php
$email = $_POST['email'];

//Aqui é uma função que irá se conectar a base e verificar se existe o e-mail ou não. 
//Você pode substituir isso pelo seu método de validação
$check = checarEmail($email);

if($check){
    echo '1';
}else{
    echo '0';
}
?>

Remember that the above example is very basic and has no security. I recommend you work on data security coming from the form to avoid SQL injections and also work on error handling.

I hope I have helped.

Good luck!

    
16.08.2016 / 15:51
0

You only need a method controlling this (secure) request . Try this:

CONTROLLER:

function check_email() {
  $this->load->library('form_validation');
  $this->form_validation->set_rules('email','trim|required|valid_email');
  if ($this->form_validation->run() === FALSE) {
    show_error('Dados inválidos!');
  }
  else{
    echo json_encode($this->nome_model->check_email());
 }
}

MODEL:

function check_email(){
    $email = $this->db->escape_str($this->input->post('email'));
    $this->db->where('email',$email);
    $query = $this->db->get(self::TABELA);
    if(!empty($query->result_array())){
        return TRUE;
    }
    return FALSE;
}

JQuery:

$('#id_do_botao').click(function(){
 $.post("nome_do_controller/check_email", {
 email: $('#con_email').val()
},
 function (resp) {
  var response = jQuery.parseJSON(resp);
  if(response === 'true'){
   $('#id_do_modal').modal('show');
  }
 });
}

Basically: the onclick event of the button calls the 'check_email ()' method of the controller via POST by passing the value of the 'con_email' field; The controller responds 'true' or 'false' based on the 'check_mail ()' method of the model; JQuery uses the answer to show or not show its modal.

    
21.09.2016 / 17:37