开发者

Notify user his password has been changed via text message on the same page (with PHP or AJAX?)

Here is the scenario. A user wants to change his password. So he login and fill up his new desired password and clicked submit. Back end PHP checks his codes and updated it in MySQL database. User is brought bac开发者_StackOverflow中文版k to the same page (with the form) and is notified by this small paragraph sitting on the top of the page that his password has been changed successfully.

Is it possbile to do this with PHP only (without jQuery)?

Apologies if I had phrased my question poorly.


You can also do without use of AJAX.

you can do this by maintaining some flags in session. when user's password is changed at back end , set $_SESSION['passwordChanged'] = true.

On start up of page check whether $_SESSION['passwordChanged'] is set TRUE then display a DIV that your passwordd is changed and reset session variable $_SESSION['passwordChanged'] = false;


Yes, it is possible by using AJAX. If the PHP backend returns a success, update a div (or any other element for that matter) to display successful password change.


If you want this work without JavaScript too, then set a session variable. So in your logic, you would have (pseudo code):

$sql = "UPDATE user SET password = ?";
$db->prepare($sql);
$db->execute(array($_POST['password'])); // password would also be hashed, presumebly
if ($db->rowCount()==1) {
    $_SESSION['password_changed'] = true;
}

Then on your page you redirect your user to, this in the header:

if ($_SESSION['password_changed']) {
    $message = "";
    unset($_SESSION['password_changed']); // no longer needed
}
if (strlen($message) > 0) {
    echo '<p class="message">' . $message . '</p>';
}

Then with JavaScript, hide the message on page load and do what you want with it, whether it's fading the message in, or dropping the message down etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜