I have some php code where I want a user to enter their email address, check it against the database then send the username associated with that email address to the user. Basically a 'forgot username' feature.
I have the following code so far, which will send en email but inside the email it reads Your username is: And that's it. The $username variable isn't being passed to the email for some reason. I am new enough to PHP so any help would be greatly appreciated.
<?php
if(isset($_POST['submit'])){
$email = $_POST['email'];
if($email==''){
echo "<script>alert('Please enter email address!')</script>";
exit();
}
$sql=mysql_query("select user_name from users where user_email='$email'");
if(mysql_num_rows($sql)>=1)
{
//echo "<script>alert('Username $user_name already exist in our database, please try another one!')</script>";
$username = $row[0];
$subject = 'Forgotten Username';
$message = 'Your username is: '.$username.'';
$header = 'Header';
if($_POST){
mail($email, $subject, $message, $header);
$feedback = 'Thanks for contacting us! We will be in contact soon!';
}
}
else {
echo "<script>alert('Email doesn't exist')</script>";
}
}
?>
HTML
<form method='post' action='forgotusername.php'>
<table width='400' border='5' align='center'>
<tr>
<td align='center'>Enter your email address</td>
<td><input type="text" name="email" style="width: 200px;" /></td>
</tr>
</table>
<input type='submit' name='submit' value='Post' />
</form>