error in script with jquery and php (validate form)

0

You see I'm using the plugin - > link to validate a form but I can not understand what I'm doing wrong to verify the user if it exists according to the console is a jquery error

$(document).ready(function() {

$("#ok").hide();

$("#signup").validate({
    rules: {
        username: { required: true, minlength: 3,
            remote: {url: "pages/registro.php", type: "post", 
                data:{
                    username: function(){return $("#username").val();}
                }
            }
        },
        pass: { required: true, minlength: 7},
        passR: {equalTo: "#pass"},
        nombre: { required:true},
        codigoB: { required:true, minlength: 7, maxlength: 7, digits: true},
        email: { required:true, email: true},
        edad: { required:true},
        pais: { required:true}
    },
    messages: {
        username: 'min. 3 caracteres.',
        pass: "min. 7 caracteres .",
        passR: "algo esta mal!",
        nombre : "Requerido.",
        codigoB : "número de 7 dígitos.",
        email : "Debe introducir un email válido."
    },
    submitHandler: function(form){
        var dataString = 'username='+$('#username').val()+'&pass='+$('#pass').val()+'&nombre='+$('#nombre').val()+'&codigoB='+$('#codigoB').val()+'&email='+$('#email').val()+'&edad='+$('#edad').val()+'&pais='+$('#pais').val();
        $.ajax({
            type: "POST",
            url:"pages/registro.php",
            data: dataString,
            success: function(data){
                $("#ok").html(data);
                $("#ok").show();
                $("#signup").hide();
            }
        });
    }
});
});

and the php code

if(isset($_POST['username'])){
        $user = $link->real_escape_string($_POST['username']);
        if(!validar_usuario($user)){echo 'true';}else{echo 'false';}    
    }
    
asked by crt 18.07.2018 в 12:15
source

1 answer

0

Ajax does not allow you to send strings as a parameter, but you have to send it as an object. You will have to change the way to send the data to this form:

$.ajax({
            type: "POST",
            url:"pages/registro.php",
            data: {"username": dataString },
            success: function(data){
                $("#ok").html(data);
                $("#ok").show();
                $("#signup").hide();
            }
        });

On the other hand I do not understand very well why you create the dataString variable with the "&". But that could give you some bug in the php later.

    
answered by 18.07.2018 в 12:32