javascript - JQuery is not picking up echo from php -
i'm echoing message ('ok') php script jquery ajax call.
i'm echoing out correct message, , showing in console when log it, appropriate jquery function not firing - according code should password has been changed successfully" message, "there problem" message - can suggest reason why?
here code first php:
if(isset($_post['oldpass'])){ $oldpass = mysql_real_escape_string($_post['oldpass']); $newpass = mysql_real_escape_string($_post['newpass']); $sql = "select password, salt users email='$log_email' , id='$log_id' limit 1"; $query = mysqli_query($db_conx, $sql); $numrows = mysqli_num_rows($query); if($numrows > 0){ while($row = mysqli_fetch_array($query, mysqli_assoc)){ $current_salt = $row["salt"]; $db_pass = $row["password"]; } $old_pass_hash = crypt($oldpass, $current_salt); if ($old_pass_hash != $db_pass){ echo "problem"; exit(); } } $s = "$2a$10$"; $random = randstrgen(20); $salt = $s.$random; $p_crypt = crypt($newpass, $salt); $sql = "update users set password='$p_crypt', salt='$salt' email='$log_email' , id='$log_id' limit 1"; $query = mysqli_query($db_conx, $sql); if ($query == true){ $_session['password'] = $p_crypt; echo 'ok'; exit(); } } ?>
this javascript/jquery
function change_password(){ var oldpass = $('#old_pass').val(); var newpass = $('#new_pass').val(); var newpass_conf = $('#confirm_new_pass').val(); if(newpass != newpass_conf){ $('#status').html("your passwords not match"); } else if(newpass=="" || oldpass==""){ $('#status').html("you have not entered anything"); } $.ajax({ type: 'post', url: "changepassword.php", datatype: 'text', data: { "oldpass": oldpass, "newpass": newpass_conf }, success:function(data){ if(data == "ok"){ $('#change_password_form').html("<h2> success</h2><div class='noerror'><p> password has been changed successfully.</p> <p> may close window.</p></div>"); } else { $('#status').html("there problem"); } } }); } $(document).ready(function(){ $(document).on('click','#change_password', function(){ change_password(); }); }); </script>
and html
<div> <h1>change password</h1></div><hr> <form id="change_password_form" class="input" onsubmit="return false;"> <div> <label for="old_pass">current password:</label> <input id="old_pass" type="text" class="searchbox" onfocus="emptyelement('status')" maxlength="88" value=""></div> <div> <label for="new_pass">new password:</label> <input id="new_pass" class="searchbox" type="text" onfocus="emptyelement('status')" maxlength="88" value=""> </div> <div><label for="confirm_new_pass">confirm new password:</label> <input id="confirm_new_pass" class="searchbox" type="text" onfocus="emptyelement('status')" maxlength="88" value=""><div> <input type="button" style="position:relative;top:10px; float:right;" id="change_password" value="change password"></form> <span id="status" class="statuserror"></span> </body> </html>
change datatype: "json"
in ajax call
then in php code return json data
json_encode(array('response'=>'ok'));
your ajax success function should this,
success: function (data) { var resultobject = jquery.parsejson(data); if(rersultobject['response']=='ok') { $('#change_password_form').html("<h2> success</h2><div class='noerror'><p> password has been changed successfully.</p> <p> may close window.</p></div>"); } else { $('#status').html("there problem"); } } }`
here parsejson
used convert json string javascript object.
Comments
Post a Comment