write files to the database with ajax + jquery

Asked

Viewed 1,026 times

0

I’m not being able to save data in mysql database with ajax, can anyone help? Not error or anything, just not saved. PHP:

$serve = mysql_connect('localhost', 'root', '');
if(!$serve){ echo 'erro';}
$db = mysql_select_db('observatoriogaia', $serve);

if($_GET['acao']=='btnCadCadastra'){

 $nome = $_GET['nome'];
 $email = $_GET['email'];
 $senha = $_GET['senha'];

$SQL = "INSERT INTO usuarios (nome, email, senha)
VALUES ('$nome','$email','$senha')";

$re = mysql_query($SQL, $serve);
}

Javascript:

var $server;
    $server = 'http://localhost/';

$(document).on("click", "#btnCadCadastra", function(evt)
{
        $nome = $('#idNome').val();
        $email = $('#idEmail').val();
        $senha = $('#idSenha').val();
                    $.ajax({
                        type: "get",
                        url: $server+"/conecta.php",
                        data: "nome="+$nome+"&acao=btnCadCadastra",
                        data: "email="+$email+"&acao=btnCadCadastra",
                        data: "senha="+$senha+"&acao=btnCadCadastra",
                        success: function(data) {
                            intel.xdk.notification.alert('Usuário cadastrado'); 
                        }
                    });
});

HTML:

<div class="upage-content ac0 content-area vertical-col left" id="page_72_85">
                <div class="table-thing with-label widget uib_w_8 d-margins" data-uib="jquery_mobile/input" data-ver="0">
                    <label class="narrow-control label-top-left" for="idNome">Nome de Usuário</label>
                    <input class="wide-control" placeholder="Ex: Gabriel" type="text" id="idNome" name="nome">
                </div>
                <div class="table-thing with-label widget uib_w_9 d-margins" data-uib="jquery_mobile/input" data-ver="0">
                    <label class="narrow-control label-top-left" for="idEmail">Email:</label>
                    <input class="wide-control" placeholder="seuemail@..." type="email" id="idEmail" name="email">
                </div>
                <div class="table-thing with-label widget uib_w_10 d-margins" data-uib="jquery_mobile/input" data-ver="0">
                    <label class="narrow-control label-top-left" for="idSenha">Senha</label>
                    <input class="wide-control" placeholder="**********" type="password" id="idSenha">
                </div>
                <div data-role="controlgroup" class="uib-jqm-flex widget uib_w_11 d-margins" data-uib="jquery_mobile/button_group" data-ver="0" data-type="horizontal">
                    <a class="widget uib_w_12" data-uib="jquery_mobile/button" data-ver="0" data-role="button" data-icon="delete" data-mini="true" id="btnCadCancela">Cancelar</a><a class="widget uib_w_14" data-uib="jquery_mobile/button" data-ver="0"
                    data-role="button" data-mini="true" data-icon="check" id="btnCadCadastra">Cadastrar</a>
                </div>
            </div>

  • Problem solved! My lack of attention did not let me see that I should import the javascript file after importing the plugins from Jquery Mobile, which resulted in error that is, the action was not being executed... Thanks For the help!

1 answer

1

I think you’re formatting this data wrong. Use it like this:

data: {
    acao: 'btnCadCadastra',
    nome: $nome,
    email: $email,
    senha: $senha
},

that jQuery does the rest, that is, put everything in one query string.

  • I made this change and nothing... stop the button does not perform the action, because nothing happens

  • 1

    @G.Vilela going by parts: you have some error in the console? what gives var_dump($_GET); in PHP? <- you probably need to see this answer inside ajax or dev-tools (F12)

  • again, nothing... nothing happens by clicking the button...

  • Solved: missing & after the first variable passed, example: date: "name="+$name+"&email='email'&action=btnCadCadastra"

Browser other questions tagged

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