I have the following jQuery that POSTS data to a form to allow a user to change their password.
$(function() {
$("#frmChangePW .submit").click(function() {
var old_pw = $("#old_pw").val();
var new_pw = $("#new_pw").val();
var uid = $("#form_id").val();
var dataString = 'form_id='+ uid + '&old_pw='+ old_pw + '&new_pw=' + new_pw;
//var dataString = $("#frmChangePW").serialize();
$.ajax({
type: "POST",
url: "change-pw.php",
data: dataString,
success: function(response) {
if(response == 1){
$('#tools_pw2').append("<font size='1' color='red'><b>Success!</b></font>");
}else{
$('#tools_pw2').append("an error occured");
}
}
});
return false;
});
});
And here is the code for change-pw.php ... Basically get the user id, their existing pw, and their new pw, hash both. Compare the id and the hash of the current password to make sure they are allowed to update the record, then update the record.
// Get variables and update the user's password
$id = $_POST['form_id'];
$cur = mysql_real_escape_string($_POST['old_pw']);
$new = mysql_real_escape_string($_POST['new_pw']);
$enc_cur = sha1($cur);
$enc_new = sha1($new);
$q = "UPDATE `users` SET `pw` = '".$enc_new."' WHERE `id` = '".$id."' AND `pw` = '".$enc_cur."'";
$r = mysql_query($q) or die(mysql_error());
$num = mysql_affected_rows();
The problem I am having is I allow users to have special characters such as $ or ! or # in their passwords, in fact I require it for complexity purposes. But it seems that some of the characters are completely ignored and in some cases, everything after it is ignored. Example, I created a user 'jdoe' with password = 123!#.
The result is that $cur becomes set to 123! and the # is completely ignored, and then $new never gets set because everything after # is just discarded.
I did some reading on stackOverflow that by using POST these issues of special characters should be solved. It didn't work so I tried using the .serialize() -- you'll see it's commented out above because that too didn't work.... I then tried urldecode() and htmlspecialchars_decode(). Both to no avail.
So what do I need to do to properly POST variables containing special characters to my pw update script so that the variables are set properly and life can go on?
TIA.