I am trying to implement a ‘reset password’ page for my website. So far I have a simple page with a form for the user to enter their email address. Submitting the form calls ‘requestpasswordreset.php’ which does things such as checking if there exists a user with that email, sending the password reset email out etc.
<body>
<form name="input" action="requestpasswordreset.php" method="post">
Email Address: <input type="text" name="email"> </br>
<input type="submit" value="Submit"></form>
</body>
requestpasswordreset.php pseudocode
if no user with email = $_POST['email']
echo 'No user exists with this email';
else
...
doStuff();
echo 'Password reset email sent';
I would like to know how I could do the following:
If the user entered an invalid email address, the form would remain visible and an error message would be displayed.
If the request was successfull the form would be hidden, and a message saying something like ‘success’ would be displayed.
It seems like it would be unnecessary/bad practice to redirect to a new page in either case? rather than just updating the current one.
It is not bad practise to redirect, or in other way make a postback, at all. The solution you are looking for is called AJAX, and both solutions are equally viable. You could use javascript (and maybe jquery) to query the server, and then hiding the form/give an error message depending on the servers response.
You cannot validate or send emails without a postback in html and php only, if that is your question.