Insert with ajax and php

0

I'm trying to do an insert with ajax and I'm not getting it. I understood that to do this I need 3 files

  • html
  • javascript (ajax)
  • php (bank)
  • part 1 and 2 is ready and working 100% but part 3 seems to me that I have to reload everything again eg: include ("db.php"), ("function.php") I car in the beginning of the system with autoload if I will call everyone inside the php file again gets complicated and not at all feasible

    Follow my codes.

    html

    <form action=""  id="form" name="form" method="post">
        <br>
        <div class="form-group">
            <label for="">Ocorrência</label>
            <input type="text" required="" value="" placeholder="" name="item[no_boletim]" id="no_boletim" class="form-control " onblur="" onclick="">
            <br>
        </div>
        <div class="form-group"> 
            <label>Descrição da Ocorrência</label>
            <textarea placeholder="" id="ds_boletim" name="item[ds_boletim]" class="form-control" rows="5"></textarea>
        </div>
        <button class="btn btn-primary" name="" type="submit">Salvar</button>
    </form>
    

    javascript

    $("#form").submit(function (e) {
        e.preventDefault();
        var array;
        array = new Array("");
        var ar = {};
    
        ar["acao"] = "incluir";
        $("#form input").each(function () {
            var nome = $(this).val();
            var chave = this.id;
            ar[chave] = nome;
    
        });
        $("#form textarea").each(function () {
            var nome = $(this).val();
            var chave = this.id;
            ar[chave] = nome;
    
        });
    
    
    
        var jsonString = JSON.stringify(ar);
        console.log(jsonString);
        $.ajax({
            type: 'POST',
            url: 'model/acao/boletimAcao.php',
            data: {item: jsonString}
        }).done(function (html) {
            console.log(html);
    
        });
    
    });
    

    php newsletterAction.php

        <?php
    
        $ar = $_REQUEST['item'];
        $ar = json_decode($ar,true);
    
        var_dump($ar['acao']);//Dados chegando tudo ok.
        //die();
        $form = new GFormControl(); // parece que o sistema não está vendo a classe
        $oquefazer = new boletim(); // aqui está meu crud que é incluido através do autoload. parece que o sistema não está vendo a classe
    
    // essa parte falta modificar para o ajax funcionar. 
    
        $acao = $_REQUEST["acao"];
        $pagina = $_REQUEST["pagina"];
        action = '?pagina=' . $pagina . '&acao=Incluir';
        actionAlterar = '?pagina=' . $pagina . '&acao=Alterar';
    
        switch ($acao) {
            case "listar";
                $listar = $oquefazer->listarAll();
                include "view/listar/boletim.php";
                break;
            case "form";
                include "view/incluir/boletim.php";
                break;
            case "Incluir";
        //        $oquefazer->incluir();
        //        $listar = $oquefazer->listarAll();
        //        redirecionar("index.php?pagina=$pagina&acao=listar");
                include "view/incluir/boletim.php";
                break;
            case"formAlterar";
                $listarU = $oquefazer->listarUm();
                include "view/alterar/boletim.php";
                break;
            case"Alterar";
                $listarU = $oquefazer->alterar();
                $listar = $oquefazer->listarAll();
                redirecionar("index.php?pagina=$pagina&acao=listar");
                break;
            case"excluir";
                $oquefazer->excluir();
                $listar = $oquefazer->listarAll();
                redirecionar("index.php?pagina=$pagina&acao=listar");
                break;
            default:
                $oquefazer->listarAll();
                include "control/listar/boletim.php";
                break;
        }
    

    My crud (model)

    <?php
    
    class boletim extends Crud {
    
        protected $table = "tb_boletim";
        protected $orderby = "cd_boletim";
    
        public function listarAll() {
            $join = array(" order by cd_boletim desc");
            $ar = $this->selectAll("*", $this->table, $join, "");
            return $ar;
        }
    
        public function listarUm() {
            $join = array("");
            $ar = $this->find(getUrl("cd_boletim"), $this->table, $join);
            return $ar;
        }
    
        public function incluir() {
            $dados = $this->dados();
            $dados['dt_boletim'] = date("Y-m-d H:i:s");
            self::insert($this->table, $dados);
            self::commit();
        }
    
        public function excluir() {
            self::delete(getUrl("cd_boletim"), $this->table);
            self::commit();
        }
    
        public function alterar() {
            self::update($this->table, $this->dados());
            self::commit();
        }
    
        public function dados() {
            $dados = $_REQUEST ["item"];
            return $dados;
        }
    
    }
    
        
    asked by anonymous 07.03.2016 / 15:16

    1 answer

    0

    You need to include the classes in the file requested by ajax. Because ajax requests only that file, you can not create objects that are not being included in the php file.

        
    08.03.2016 / 21:06