Problems When passing data via ajax

Asked

Viewed 179 times

0

I have a form where it is validated by jqBootstrapValidation.js, and is sent to another file, which is the file below:

// Contact Form Scripts

$(function() {

    $("#usuariosForm input,#usuariosForm textarea").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function($form, event, errors) {
            // additional error messages or events
        },
        submitSuccess: function($form, event) {
            event.preventDefault(); // prevent default submit behaviour
            // get values from FORM
            var txtnome = $("input#txtnome").val();
            var ememail = $("input#ememail").val();
            var txtusuario = $("input#txtusuario").val();
            var pwdsenha = $("input#pwdsenha").val();
            var urltxt = $("input#urltxt").val();

            var firstName = name; // For Success/Failure Message
            // Check for white space in name for Success/Fail message
            if (firstName.indexOf(' ') >= 0) {
                firstName = name.split(' ').slice(0, -1).join(' ');
            }

            $('#submit').html("...Enviando");
            $('#submit').prop("disabled", true);

            $.ajax({
                url: urltxt,
                type: "POST",
                data: {
                    txtnome: txtnome,
                    ememail: ememail,
                    txtusuario: txtusuario,
                    pwdsenha: pwdsenha
                },
                cache: false,
                success: function() {
                    // Success message
                    $('#success').html("<div class='alert alert-success'>");
                    $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-success')
                        .append("<strong>Registro realizado com sucesso</strong>");
                    $('#success > .alert-success')
                        .append('</div>');

                    //clear all fields
                    $('#usuariosForm').trigger("reset");
                    $('#submit').html("Enviar mensagem");
                    $('#submit').prop("disabled", false);

                },
                error: function() {
                    // Fail message
                    $('#success').html("<div class='alert alert-danger'>");
                    $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-danger').append($("<strong>").text("Desculpe " + firstName + ", ocorreu um erro. Por favor tente novamente!"));
                    $('#success > .alert-danger').append('</div>');
                    //clear all fields
                    $('#usuariosForm').trigger("reset");
                },
            });
        },
        filter: function() {
            return $(this).is(":visible");
        },
    });

    $("a[data-toggle=\"tab\"]").click(function(e) {
        e.preventDefault();
        $(this).tab("show");
    });
});


/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
    $('#success').html('');
});

But I’m in trouble, as I have several formularies I wanted to always use the same function. and pass the url to it to do sql correctly, IE, if I pass the cadas_usuarios page it will do user registration. if it passes update_usuario it goes to the user change page. I tried a method of passing the url to ajax as follows:

<input type="text" name="urltxt" id="urltxt" class="form-control text-center" value="?folder=usuarios/&file=ins_usuarios_usuarios&ext=php" data-validation-required-message="Por favor não mude os dados." required>

And in the file it would receive in a variable, but it’s not working. How can I do that? Thank you! NOTE: The nomenclature of my pages are: ? Folder=usuarios/&file=ins_usuarios_usuarios&ext=php

I debug the code see:

inserir a descrição da imagem aqui

  • Gives some error in the console?

  • @Aline do not miss any :C

  • And to which page are you sending this post? Can debug this js?

  • @Aline Work with a dynamic upload, the page link would be: ? Folder=usuarios/&file=ins_usuarios_usuarios&ext=php

  • Um... and if there is no error in js, is it coming to the page? You can debug?

  • @Aline for what I saw this not arriving in the page.

  • @Aline, I will debug the code and the url variable returns Undefined, put a print on the question

  • I suggest you change the url that is in value to a: data-url. and remove the value property. where you take the value, test: . data("url"), instead of . val(). You can test?

  • I would use the data-url in the right form? and where I get the value would look like var urltxt = ?

  • <input type="text" name="urltxt" id="urltxt" class="form-control text-center" data-url="? Folder=usuarios/&file=ins_usuarios_usuarios&ext=php" data-validation-required-message="Please do not change the data." required> and in js: var urltxt = $("input#urltxt"). data("url");

  • Aline worked well, thank you very much. I have a doubt, as in the example last above is a user record. Isn’t it too insecure for me to pass the decrypted password through the ajax? Is there any way I can encrypt in ajax and decrypt when I get to the PHP page

  • I’ll add in the answer then you can mark, okay? =)

Show 7 more comments

1 answer

1


Replace the value of Inp:

<input type="text" name="urltxt" id="urltxt" class="form-control text-center" data-url="?folder=usuarios/&file=ins_usuarios_usuarios&ext=p‌​hp" data-validation-required-message="Por favor não mude os dados." required> 

And in Js get the new property:

var urltxt = $("input#urltxt").data("url");

In the case of security of information: If it is a user registration, you will pass the password that he set and encrypt on the server, from there, ngm plus, besides the user, you will know the password. (Of course this will depend on the encryption algorithm used). In the case of user editing, will already come encrypted, so it is quiet.

Encrypt in js: What’s the point? If I can see your js code? (=

Browser other questions tagged

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