0

I am writing a thing to compare two passwords with each other, if they match the script sends out a response that says it is the same.

I currently have got this code:

$("#repeatPw").keyup(function(){
    jQuery.ajax({
        url: "System/Javascript/Functions/checkPasswords.php",
        data: "'password1'='" + $("#Password").val() + "', 'password2'='" + $("#repeatPw").val() + "'",
        type: "POST",
        success: function(data) {
            $("#passwordMatch").html(data);
        },
        error: function(data) {}
    });
});

Now my problem is that i cant get this password1 and password2 in a proper array i can explode in the checkPasswords.php, this posts this:

Array ( ['password1'] => 'fasfasdfasSD2', 'password2'='asdasdasd' )

But this is not a proper array as it only puts password1 in proper array format, how would i go about making password2 in this format too?

Thank you all in advance!

2
  • 1
    I wonder why do you have to check this in your server? You could have validated in the client side itself. Commented Jul 5, 2017 at 9:57
  • in js, you create arrays like so : var array = [ ]; Then you can add item in it with push(). Another way could be to create a JSON object. data : {'password1' : $("#Password").val(), 'password2' : $("#repeatPw").val() } Commented Jul 5, 2017 at 9:57

4 Answers 4

1

You can do it with a FormData object:

$("#repeatPw").keyup(function(){
  var fd = new FormData();
  fd.append('password1', $("#Password").val());
  fd.append('password2', $("#Password").val());
  jQuery.ajax({
    url: "System/Javascript/Functions/checkPasswords.php",
    data: fd,
    type: "POST",
      success: function(data) {
        $("#passwordMatch").html(data);
    },
    error: function(data) {}
  });
});

Or do it the JSON way:

$("#repeatPw").keyup(function(){
  jQuery.ajax({
    url: "System/Javascript/Functions/checkPasswords.php",
    data :{
            password1: $("#Password").val(),
            password2: $("#repeatPw").val(),
            },
    type: "POST",
    success: function(data) {
        $("#passwordMatch").html(data);
    },
    error: function(data) {}
  });
});
Sign up to request clarification or add additional context in comments.

Comments

0

Create an array and pass it as the ajax post data,

   var data=[];

    data['password1']= $("#Password").val();
    data['password2']= $("#repeatPw").val();

Though you could do this in the client side itself.

if($("#Password").val().trim() == $("#repeatPw").val().trim())
    //password Matches

Comments

0

Hopes this help u..

$("#repeatPw").keyup(function(){
  jQuery.ajax({
    url: "System/Javascript/Functions/checkPasswords.php",
    data :{
            password1: $("#Password").val(),
            password2: $("#repeatPw").val(),
            },
    type: "POST",
    success: function(data) {
        $("#passwordMatch").html(data);
    },
    error: function(data) {}
  });
});

Comments

0

Try like this

$("#repeatPw").keyup(function(){
jQuery.ajax({
    url: "System/Javascript/Functions/checkPasswords.php",
    data: {'password1' : $("#Password").val(), 'password2' : $("#repeatPw").val() },
    type: "POST",
    success: function(data) {
        $("#passwordMatch").html(data);
    },
    error: function(data) {}
});
 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.