2

when i click in the button btnSalvar, ajax need to send data to my controller, but not working.

my ajax:

$("#btnSalvar").on('click', function(){

                    $.ajax({
                            url: '<?= base_url(); ?>' + 'grupoProduto/cadastro',
                            type: 'POST',
                            data: {Nome: $("#txtNome").val(), Ativo: $("#cbxAtivo").val()},
                            dataType: 'json',
                            cache: false,
                            success: 
                                      function(data){
                                        alert(data.Nome);  //as a debugging message.
                                      }
                        });
                    return false;

    });

My controller:

public function cadastro() { $this->load->view('grupoproduto_view'); }

my alert(data.Nome) showing nothing

here's my full php code:

`

    <div class="row">
        <div class="form-group">
            <label for="txtNome" class="col-md-3 control-label">Nome</label>
            <div class="col-md-6">
                <?php echo form_input(['name' => 'txtNome', 'class' => 'form-control', 
                'value' => set_value('txtNome'), 'required' => 'true', 'maxlength' => '50', 'required' => 'true', 'id' => 'txtNome']); ?>
            </div>
        </div>
        <div class="form-group">
            <label for="cbxAtivo" class="col-md-3 control-label">Ativo</label>
            <div class="col-md-1">
                <?php echo form_checkbox(['name' => 'cbxAtivo', 'class' => 'form-control', 'required' => 'true', 'id' => 'cbxAtivo']); ?>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-12">
            <div class="form-group">
                <div class="modal-footer">
                    <?php echo anchor(NULL, "<i class='glyphicon glyphicon-remove'></i> Sair", ['class' => 'btn btn-default', 'id' => 'btnSair', 'role' => 'button', 'data-dismiss' => 'modal']); ?>

                    <?php echo form_button(NULL, "<i class='glyphicon glyphicon-ok'></i> Salvar", ['class' => 'btn btn-success', 'id' => 'btnSalvar', 'role' => 'button']); ?>
                </div>
            </div>
        </div>
    </div>

</fieldset>

new edit!

      `$("#btnSalvar").on('click', function(){

        var nome = $("#txtNome").val();
        var ativo = $("#cbxAtivo").val();

        var url = '<?= base_url(); ?>grupoProduto/cadastro';

        $.ajax({
                url: url,
                type: 'POST',
                data: {Nome: nome, Ativo: ativo},
                dataType: 'json',
                cache: false,
                success: 
                          function(data){
                            alert(data.Nome);  //as a debugging message.
                          },
                error: function() { 
                    alert(url);
                }     
            });
        return false;

    });`

return error in the ajax and show this url: http://localhost/admin/grupoProduto/cadastro. It's correct, but why not success?

HawkB
  • 171
  • 1
  • 2
  • 10
  • Have you check the browser console for errors? – kishor10d May 25 '17 at 06:47
  • check the network console what is the error. – Shubham May 25 '17 at 06:48
  • i've checked and no errors – HawkB May 25 '17 at 06:49
  • should i expect params in the controller function? – HawkB May 25 '17 at 06:51
  • `$this->load->view();` load the page. `ajax` doesn't load pages, rather used for small request to server and get small piece of response data from server. In your function `cadastro()` you didn't receive any data what you have sent. e.g `{Nome: $("#txtNome").val(), Ativo: $("#cbxAtivo").val()}`. Additionally you use `dataType: 'json',` in your ajax function indicate that you expect a response from server in json format.If you send data from server that is not json, then you got an alert `undefined`. – Ataur Rahman Munna May 25 '17 at 06:55
  • Check network and get the response coming – chamina May 25 '17 at 06:56

2 Answers2

2

Your PHP '<?= base_url(); ?>' + 'grupoProduto/cadastro' is not getting evaluated

 $.ajax({
        url: '+<?= base_url(); ?>+'+ 'grupoProduto/cadastro',
        ....

Problem is in your base_url()

In order to use base_url(), you must first have the URL Helper loaded. This can be done either in application/config/autoload.php (on or around line 67):

$autoload['helper'] = array('url');

Or,

manually:

$this->load->helper('url');

checkout this answer

Nadir Laskar
  • 4,012
  • 2
  • 16
  • 33
1

maybe the problem as Nadir says that base_url not evaluated ,

you can try something like this

var link = "<?php echo base_url(); ?>";
var Nome = $("#txtNome").val();
var Ativo = $("#cbxAtivo").val();
$.post(link + "grupoProduto/cadastro", {Nome : Nome ,Ativo :Ativo},function(data){
    alert("something")
});