Doubt about ajax

Asked

Viewed 65 times

1

My question is whether this code of mine 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 reload the updated content again, however is on the same page where already this.

I have an Admin page, where I click to 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 list all users already registered and give the option to register new user, if chosen, this option opens a modal, where I register new User, however this modal remains 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>

2 answers

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');
   }
});
  • Just like, I want to do it in a way that only uses ajax, and it loads this same page

  • It may be the same. As long as it is identical to your formatting. But it does not need the include. The load already loads the page.

  • He will carry the same thing inside my main ? Because this page is already inside a main one, and if I upgrade it goes back to home from the main one.

  • Oh Bro. You have a Div#List. Okay? Inside this Div you carry a Include for what I see. It’s the Users list. Beauty. When you register someone new and close the modal you want to reload this list with the new list with the new registered user. Right ? Then, you can use the same Include page. Put it on Load. And put a cache:false in Ajax.

  • Better yet, Dude. Put in your ajax a Success.

  • how would this look ?

  • I edited my Code.

  • Thank you, get success with your help. Now I would like you to help me in the delete part. has like ?

  • Yes. Try something and tell us. Maybe even a new guy.

  • http://answall.com/questions/67212/delete-no-banco-utilizando-ajax-e-jquery The link to my other question.

Show 5 more comments

1


These lines are not at all correct:

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

Variables in javascript are typed dynamically, you cannot fix a type. To declare a variable locally, use the var:

var lista_cont = "lista_cont é uma string agora";

If you want to declare public property of instance, 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, because the variable lista_cont was declared locally

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

On the functioning of the code:

Cannot insert PHP code this way into HTML elements:

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

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

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

The method load PHP loads the HTML from the server and puts it in the selected element.

  • Thank you for your contribution.

  • can help me with deleting ? how do I send the table id ?

  • You can use the data attribute of the element that contains the user from the list by clicking delete this attribute and calling a PHP function via ajax. On success remove HTML. However I suggest creating a new question with this specific question, containing the HTML of your user list.

  • http://answall.com/questions/67212/delete-no-banco-utilizando-ajax-e-jquery

Browser other questions tagged

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