Jump to content

PHP script to send confirmation email


Themattyc

Recommended Posts

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.

 

 

Link to comment
Share on other sites

here is your problem taken from php.net

 

<?php
$var1 = TRUE;
$var2 = FALSE;

echo $var1; // Will display the number 1

echo $var2; //Will display nothing

/* To get it to display the number 0 for
a false value you have to typecast it: */

echo (int)$var2; //This will display the number 0 for false.
?>

 

Note setting a var to true means you are actually setting it to the value 1 not the word true, so when you check if it is true before you connect to your database checking if $formOK==true would automatically goto your else statement because $formOK has been set to 1.

 

hope that helps

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.