CodeIgniter Forums

Full Version: [Solved] Validate whether data entered in the input exists in the database - JQUERY
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:
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 class="form-group col-md-4">     
<label for="cletivo">Ano</label>
    <input type="text" class="form-control" id="cletivo" name="cletivo" placeholder="Ano">
<div class="form-group col-md-4">     
<label for="csemestre">Semestre</label>
     <input type="text" class="form-control" id="csemestre" name="csemestre" placeholder="Semestre">
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')); ?>
Footer view file:
PHP Code:
            $('#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')
                    alert("tudo funcionando 100%.");
                    alert("NÃO funcionando !!!!!!!!!!!");


PHP Code:
public function verifica_turma() 
alert(" FUNCIONA CABRUNCO !!!!!!!!!!!!!!!!!!!! ");
        if ($this->form_validation->run() === FALSE) {
            show_error('Estão faltando dados a preencher no formulario !');
            show_error('Dados válidos!');
            echo json_encode($this->dia->check_turma());
            if ($resp==true){
                alert(" turma existe ");
                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'));
        $query $this->db->get(self::TABELA);
            return TRUE;
        return FALSE;
Do I have to change anything in 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'
The first problem was solved: I was using jquery slim and this jquery does not recognize $ .ajax
(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:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Requisicoes extends CI_Controller{
        function __construct()
            //$this->load->helper(array('form', 'url'));
public function ajaxrequestpost() 
('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);
            echo json_encode($a);

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


But, it keeps returning HTML from the header.