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'>×")
.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'>×")
.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:
Gives some error in the console?
– Aline
@Aline do not miss any :C
– Gabriel Filippi
And to which page are you sending this post? Can debug this js?
– Aline
@Aline Work with a dynamic upload, the page link would be: ? Folder=usuarios/&file=ins_usuarios_usuarios&ext=php
– Gabriel Filippi
Um... and if there is no error in js, is it coming to the page? You can debug?
– Aline
@Aline for what I saw this not arriving in the page.
– Gabriel Filippi
@Aline, I will debug the code and the url variable returns Undefined, put a print on the question
– Gabriel Filippi
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?
– Aline
I would use the data-url in the right form? and where I get the value would look like var urltxt = ?
– Gabriel Filippi
<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
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
– Gabriel Filippi
I’ll add in the answer then you can mark, okay? =)
– Aline