views:

43

answers:

3

Hi guys,

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 back 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.

+1  A: 

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.

Alan Haggai Alavi
Thanks Alan. I'm hoping to do this without Ajax.
dave
You are welcome, Dave.
Alan Haggai Alavi
+2  A: 

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;

Maulik Vora
Thanks Maulik. That's a good solution
dave
+1  A: 

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.

Martin Bean
Thanks Martin. That's a good solution.
dave