Insert with ajax and php

Asked

Viewed 207 times

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

  1. html
  2. javascript(ajax)
  3. php (bank)

part 1 and 2 are up and running 100% but part 3 seems to me I have to reload everything again ex: include("db.php"),("Function.php") i car at the beginning of the system with autoload if I call everyone inside the php file again gets complicated and no little viable

follows 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 newsletters.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;
    }

}
  • Missing the include of the classes or the file that makes the classing of them in the bulletAcao.php. Place the error that appears (you may need to see it through the javascript console or test it by calling php directly.)

1 answer

0

You need to include the classes in the file requested by ajax. Since ajax only requires that file, there is no way you can create the objects that are not being included in the php file.

Browser other questions tagged

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