• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] Validate whether data entered in the input exists in the database - JQUERY

#1
I have 3 inputs in the view. They 3 together identify a class from the school. (name, year and semester). So my idea was to validate these values before the submit button does all the data verification. 
I researched and found this example. 
Look the last example:
https://pt.stackoverflow.com/questions/1...-dados-php
But, it is not calling the controller / method.
The view´s inputs :
PHP Code:
<div class="form-group col-md-4">     
     
<label for="nturma">Turma</label>
     <input type="text" class="form-control" id="nturma" name="nturma" placeholder="Nº Turma">
</
div>
<
div class="form-group col-md-4">     
     
<label for="cletivo">Ano</label>
 
    <input type="text" class="form-control" id="cletivo" name="cletivo" placeholder="Ano">
</
div>   
<div class="form-group col-md-4">     
     
<label for="csemestre">Semestre</label>
     <input type="text" class="form-control" id="csemestre" name="csemestre" placeholder="Semestre">
 </div>   
I already checked, placing an Alert () inside the script to find out if the values of the inputs were correct.
This is a button in  a View.
PHP Code:
<div style="text-align:right">
    <button type="submit" class="btn btn-primary" id="btgravar" name="btgravar" >Gravar</button>
<
   ?= anchor('planodeaula/alertar/500''Voltar', array('class' => 'btn btn-danger')); ?>
 </div> 
Footer view file:
PHP Code:
<script>
          $(document).ready(function(){
            $('#btgravar').click(function(){$.post("<?php echo base_url();?>Planodeaula/verifica_turma",
            {
                nturma : $('#nturma').val(),
                cletivo : $('#cletivo').val(),
                csemestre : $('#csemestre').val()
                
            
},
            function (resp
            {
                var response jQuery.parseJSON(resp);
                alert("tudo funcionando 100%.");
                if(response === 'true')
                {
                    //$('#id_do_modal').modal('show');
                    alert("tudo funcionando 100%.");
                }else{
                    alert("NÃO funcionando !!!!!!!!!!!");
                }
            }    
            
});
    
    });
    </script

 Controller:

PHP Code:
public function verifica_turma() 
    {
    
alert(" FUNCIONA CABRUNCO !!!!!!!!!!!!!!!!!!!! ");
        $this->load->library('form_validation');
        $this->form_validation->set_rules('nturma','trim|required');
        $this->form_validation->set_rules('cletivo','trim|required');
        $this->form_validation->set_rules('csemestre','trim|required');
        if ($this->form_validation->run() === FALSE) {
            show_error('Estão faltando dados a preencher no formulario !');
        }
        else{
            show_error('Dados válidos!');
            echo json_encode($this->dia->check_turma());
            $resp=$this->dia->check_turma();
            if ($resp==true){
                alert(" turma existe ");
            }else{
                alert("não existe");
            }
            
        
}
    

Model :
PHP Code:
  function check_turma()
    {
        alert("Hello! I am an alert box!!");
        $nturma $this->db->escape_str($this->input->post('nturma'));
        $cletivo $this->db->escape_str($this->input->post('cletivo'));
        $csemestre $this->db->escape_str($this->input->post('csemestre'));
        $this->db->where('nturma',$nturma);
        $this->db->where('cletivo',$cletivo);    
        $this
->db->where('csemestre',$csemestre);
        $query $this->db->get(self::TABELA);
        if(!empty($query->result_array())){
            return TRUE;
        }
        return FALSE;
    
Do I have to change anything in routes.php ?
Routes.php
PHP Code:
$route['default_controller'] = 'Dashboard';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['aluno/(:num)'] = 'aluno/index/$1';
$route['turma/(:num)'] = 'turma/index/$1';
$route['curso/(:num)'] = 'curso/index/$1';
$route['disciplina/(:num)'] = 'disciplina/index/$1';
$route['professor/(:num)'] = 'professor/index/$1';
$route['diario/(:num)'] = 'diario/index/$1';
$route['dashboard/(:num)'] = 'dashboard/index/$1';
$route['senha/(:num)'] = 'senha/index/$1';
$route['administrativo/(:num)'] = 'administrativo/index/$1';
$route['notasturma/(:num)'] = 'notasturma/index/$1';
$route['Pdfexample/(:num)'] = 'pdfexample/index/$1';
$route['Planodeaula/(:num)'] = 'planodeaula/index/$1';
$route['Planodeaula/verifica'] = 'planodeaula/verifica_turma'
Reply

#2
The first problem was solved: I was using jquery slim and this jquery does not recognize $ .ajax
Reply

#3
(03-19-2020, 08:30 AM)RRIOS Wrote: The first problem was solved: I was using jquery slim and this jquery does not recognize $ .ajax

I was instructed to remove all html from the Controller that has the ajaxrequestpost method.
Every Controller of mine has, in the index, calls to header, side menu and footer. And those contain html.
So I created a Controller using only the ajaxrequestpost method.
Remembering that this method is just a draft for me to try to make $ .ajax work. See that I invented an array.
PHP Code:
<?php
     
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    
class Requisicoes extends CI_Controller{
        function __construct()
        {
            parent::__construct();
            $this->load->model('planodeaula_model','plano');
            $this->load->model('diario_model','dia');
            //$this->load->helper(array('form', 'url'));
                    
        
}
    
    
public function ajaxrequestpost() 
    {
    
      
            header
('Content-type: application/json');
            $a = array("tipo"=>"APARTAMENTO""rua"=>"BARÃO DE MIRACEMA 110 apto 902""bairro"=>"CENTRO""cidade"=>"CAMPOS DOS GOYTACAZES");
            //$a = utf8_encode($a);
            //print_r($a);
            echo json_encode($a);
            //exit;
    
}


When entering the Controller address, the return is the list in Json.

{"tipo":"APARTAMENTO","rua":"BARAO DE MIRACEMA","bairro":"CENTRO","cidade":"CAMPOS DOS GOYTACAZES"}

But, it keeps returning HTML from the header.
Reply

#4
Problem solved :

https://pt.stackoverflow.com/questions/4...758#440758
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.