Ajax saving dynamically

3

I'm trying to save the register made, but when I open my modal, it does not understand that I'm clicking save, so it does not send the data, it follows the code I'm facing problems:

Userlist.php

<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery('#cadUsuario').submit(function(){
            var dados = jQuery( this ).serialize();

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

            return false;
        });
    });
</script>
<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>
        <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
        <button type="submit" class="btn btn-primary" id="botao_cadUsuario">Salvar</button>

And the new page to actually make the registration is like this.

saveUsername.php

<?php
    // incluindo o arquivo que faz a conexao com o banco
    include ("../includes/conexao.php");

    $nome = isset($_POST['TXT_NOMEX_USUAR']) ? $_POST['TXT_NOMEX_USUAR'] : '';
    $email = isset($_POST['TXT_ENDER_EMAIL']) ? $_POST['TXT_ENDER_EMAIL'] : '';
    $senha = isset($_POST['TXT_SENHA_USUAR']) ? $_POST['TXT_SENHA_USUAR'] : '';


    $query = "INSERT INTO tbl_USUARIOS (TXT_NOMEX_USUAR, TXT_ENDER_EMAIL, TXT_SENHA_USUAR) VALUES";
    $query .=  "('$nome','$email','$senha')";

    //executando a query
    $inserir = mysql_query($query)
    or die(error());

    $response = array("success" => true);

    //fechando a conexao com o banco
    mysql_close($conn);

    echo "Cadastrado com Sucesso!";

?>

It is not a problem in SQL because it is working perfectly, the problem is that it does not recognize the click of the save button.

    
asked by anonymous 02.06.2015 / 18:52

1 answer

1

The parameter data should contain the serialized form.

<script type="text/javascript">
    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>
    
02.06.2015 / 18:59