Doubt on ajax

1

My question is whether this code is correct.

$(document).ready(function(){
                $("#fechar").click(function(e){
                  e.preventDefault();
                  String lista_Cont = $(this).attr('<?php include "listaUsuario2.php";?>');
                  $("#lista").load(this.lista_Cont);
                });
            });

The function of it is, when I close the modal, where I used to register a new user, it reloads the updated content again, but it is on the same page where it already is.

I have an admin page, where I click on register and go to users, the system in Ajax returns only the content of the changed medium, in this content comes the code where I ready all users already registered and give the option of register new user, if it is chosen, this option opens a modal, where I register new User, but this modal is still open, and when I close it, the page is not updated. I need that when I close it already update the section to min, and enable me to see new users registered immediately.

MY COMPLETE CODE:

<script type="text/javascript">
  $(document).ready(function(){
                $("#fechar").click(function(e){
                  e.preventDefault();
                  String lista_Cont = $(this).attr('<?php include "listaUsuario2.php";?>');
                  $("#lista").load(this.lista_Cont);
                });
            });


    jQuery(document).ready(function(){
            jQuery('#cadUsuario').submit(function(){
                var dados = jQuery( this ).serialize();

                jQuery.ajax({
                    type: "POST",
                    url: "salvarUsuario.php",
                    data: dados,
                    success: function( data )
                    {
                        alert( data );
                    }
                });

                return false;
            });
        }); 

</script>
<div class = "conteudo">
                <div class="container-fluid">
                    <div align="left" id="inserir">
                        <button type="button" class="btn btn-default btn-lg" data-toggle="modal" data-target="#myModal">
                            <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Inserir
                        </button>
                    </div>

                                <!-- Listar Usuarios -->
                <legend>Úsuarios Cadastrados</legend>
                <div id="lista">
                <?php include "listaUsuario2.php";?>
                </div>

                </div>
                <!-- Fim listar Usuarios -->

</div> <!-- FECHA CONTEUDO -->

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Novo Úsuario</h4>
      </div>
    <div class="modal-body">            
        <div align="center">
            <form class="form-horizontal" action="" method="post" id="cadUsuario">
            <fieldset>

                    <!-- Text input-->
                    <div class="control-group">
                      <label class="control-label" for="TXT_NOMEX_USUAR"></label>
                      <div class="controls">
                        <input id="TXT_NOMEX_USUAR" name="TXT_NOMEX_USUAR" type="text" placeholder="Nome" class="input-large">

                      </div>
                    </div>

                    <!-- Text input-->
                    <div class="control-group">
                      <label class="control-label" for="TXT_ENDER_EMAIL"></label>
                      <div class="controls">
                        <input id="TXT_ENDER_EMAIL" name="TXT_ENDER_EMAIL" type="text" placeholder="Email" class="input-xlarge">

                      </div>
                    </div>

                    <!-- Password input-->
                    <div class="control-group">
                      <label class="control-label" for="TXT_SENHA_USUAR"></label>
                      <div class="controls">
                        <input id="TXT_SENHA_USUAR" name="TXT_SENHA_USUAR" type="password" placeholder="Senha" class="input-small"> 
                      </div>
                    </div>
                    <br>
                    <br>          
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal" id="fechar">Fechar</button>
                    <button type="submit" class="btn btn-primary" id="botao_cadUsuario">Salvar</button>
                </div>
            </fieldset>
            </form>
        </div>
      </div>
    </div>
  </div>
</div>
    
asked by anonymous 02.06.2015 / 21:29

2 answers

1

These lines are not quite right:

String lista_Cont = $(this).attr('<?php include "listaUsuario2.php";?>');
$("#lista").load(this.lista_Cont);

Variables in javascript are typed dynamically, you can not set a type. To declare a variable locally, use var :

var lista_cont = "lista_cont é uma string agora";

If you want to declare a public instance property, then use this :

var minhaClasse = function() {
    this.Foo = "Uma string";

    this.imprimeFoo = function() {
        alert(this.Foo);
    }
}

var a = new minhaClasse();
var b = a.Foo;
a.imprimeFoo();

So, these lines have a syntax problem too, in this context, this does not exist, since the lista_cont variable was declared locally

$("#lista").load(this.lista_Cont); // deveria ser $("#lista").load(lista_Cont);

How the code works:

You can not insert PHP code this way into HTML elements:

$(this).attr('<?php include "listaUsuario2.php";?>');

The PHP interpreter runs on the server, before the browser receives the HTML. If you want to dynamically upgrade with ajax, you can use:

$("#lista").load("listaUsuario2.php");

The load method of PHP loads the HTML from the server and places it on the selected element.

    
02.06.2015 / 21:52
1

Create a page with a Select by searching the records and with the same formatting as your list.

And in ajax you do:

$("#lista").load('lista.php');

jQuery.ajax({
   type: "POST",
   url: "salvarUsuario.php",
   data: dados,
   success: function( data )
   {
       $("#lista").load('listausuario.php');
   }
});
    
02.06.2015 / 21:39