Log in

View Full Version : PHP script to send confirmation email



TheMattyC
02-13-2012, 11:43 PM
I am currently trying to have my PHP script perform the following:
-Verify that values have been entered in each field and show a message to the user about any missing inputs
-Verify that the 2 passwords enter match and show a message to the user if these don’t match
-Save the user to the database if all the input data is valid
-Send a confirmation email to the user upon successfully saving their profile to the database

My html form code is as follows:

<html>
<body>
<form method="post" action="save_user.php">



<div>

<label>First Name</label>

<input name="First_name" maxlength="20" />

</div>
<div>

<label>Last Name</label>

<input name="Last_name" maxlength="20" />

</div>
<div>

<label>Email</label>

<input name="Email" maxlength="100" />

</div>

<div>

<label>Password</label>

<input name="Password" type="password" maxlength="50" />

</div>

<div>

<label>Retype Password</label>

<input name="confirm_password" type="Password" maxlength="16" />

</div>
<div>

<label>City</label>

<input name="City" maxlength="15" />

</div>

<div>

<select name="Province">


<option>Select Province</option><option value="AB">AB</option><option value="BC">BC</option><option value="MB">MB</option><option value="NB">NB</option><option value="NL">NL</option><option value="NU">NU</option><option value="ON">ON</option><option value="PE">PE</option><option value="PQ">PQ</option><option value="SK">SK</option><option value="YT">YT</option></select>

</div>


<input type="submit" value="Submit" />

</form>







</body>
</html>
And my PHP code is as follows:

<html>
<body>
<?php
//create a variable we can use to determine if the user input is good or not
$formOk=true;
//Check that a first name has been entered
if (empty($_POST['First_name'])) {
echo 'First name is required';
$formOK = false;
}
//Check that a last name has been entered
if (empty($_POST['Last_name'])) {
echo 'Last name is required';
$formOK = false;
}
//Check that an email was entered
if (empty($_POST['Email'])) {
echo 'Email is required';
$formOK = false;
}
//Check that a password value has been entered
if (empty($_POST['Password'])) {
echo 'Password is required';
$formOK = false;
}
//Check for password value and if it matches the retyped password
if ($_POST['Password'] != $_POST['confirm_password']) {
echo 'Passwords do not match';
$formOK = false;
}
//Check that a city has been entered
if (empty($_POST['City'])) {
echo 'City is required';
$formOK = false;
}
//If all the information entered is valid, save the user to the database
if ($formOK == true) {
$conn = mysqli_connect('localhost', 'dbxxxxx', 'XXXXXX', 'dbxxxxx') or die('Could not connect: ' . mysql_error());

//Hash the password before saving it to make the value obscure
$passwordHash = sha1($_POST['Password']);
$sql = "INSERT INTO users (First_name,Last_name,Email, Password,City,Province) VALUES ('$_POST[First_name]','$_POST[Last_name]','$_POST[Email]','$passwordHash','$_POST[City]','$_POST[Province]')";
//Once the user has been added to the database, send a confirmation email to notify them
//Create the variables to be used in the mail
$to=$_POST['Email'];
$subject="Email Confirmation";
$message="This is a confirmation email";
//Use the mail function to send the confirmation email
mail($to,$subject,$message);
//Show the user that they have been added and close the connection to the database.
mysqli_query($conn, $sql);
echo "User has been added";
mysqli_close($conn);
}
else {
echo 'Click <a href="javascript:history.go(-1)">HERE</a> to go back and adjust your entry.';
}
?>

</body>
</html>

I am unable to figure out why my php script will need post the data into my database as it just seems to instantly jump to the else statement when i try to run it. Any help would be greatly appreciated.

jbrill1012
02-16-2012, 05:20 AM
Just a note, but your password code isn't the same...


<div>

<label>Password</label>

<input name="Password" type="password" maxlength="50" />

</div>

<div>

<label>Retype Password</label>

<input name="confirm_password" type="Password" maxlength="16" />

</div>

password and Password ... Length doesn't match as well so if they put in 17 characters for the 1st instance it wouldn't match with the second because they wouldn't be able to get enough characters. Don't see it happening but it's possible.