User Personalization with PHP:User Registration [con't]
We are only interested in the file name, so we test to see if a file has been uploaded:
if(isset($_FILES['fn'])){
If a file has been uploaded then we transfer the file name to a shorter variable and try to upload it to the images directory where we store all user images:
$fn = $_FILES['fn']['name'];
We use the move_uploaded_file()
function to send the file to its final destination:
if(!move_uploaded_file($_FILES['fn']['tmp_name'],'../images/'.$_FILES['fn']['name'].'')){
If any errors occur, then it will be stored in the FILES arrays' errors, we go through
them to see which of the errors has been encountered and set the $msg
variable
to pick up the error:
Then we test to see if the file name is set; if so, we fill the image variable with the file name. If no file has been uploaded, we set the image variable to 'No_img':
Next, we store the color that has been selected by the user in the $bgc
variable:
$bgc = $_POST['select'];
Now we are ready to insert the data into the database; we create a SQL statement and run the query:
$sql_ins="INSERT INTO users(uid,uname,upass,e-mail,level,bgc,img,actcode) values (5,'".$uname."','".md5($upass)."','".$e-mail."','".$level."','".$bgc."','".$img."','".$actcode."')";
$result = mysql_query($sql_ins);
If the user information has been inserted successfully, we need to notify the user accordingly:
Then we need to send the activation code through the email. To send the email we will use
the mail()
function of PHP. Make sure that you've made the necessary changes in your
PHP ini file, before using this function. We start by setting the subject of the email
message:
//now we notify the user through e-mail
$subject="Registration at Online Bookmarks";
Then we start to create the body of the email message:
$emsg = "Thank you for registering with us.The next step is for you to activate your account. To do this, simply click on the link below:\n\n";
We then set the activation URL and instruct the user on what to do:
If the query we run encountered any problems then we show the error:
}else{
$msg .=" The following MYSQL error occurred:".mysql_error().";
}
}else{
$msg = "Error with the e-mail address provided ";
}
}//end err check
}//end submit check
?>
The HTML Code
The HTML part of the registration form contains the main form that collects user information such as the users name, email, password and other information. The code itself utilizes three different coding languages:
- HTML - Hosting the form
- JavaScript - Clientside form data validation
- PHP - Displaying error information
The JavaScript basically checks that the form fields are completed using a function called checkform().
We've used the same function in the login form to check the log-in details of the user in
the previous article. Basically, the function tests each
required field on the form to see if it contains text. If the fields are empty then a
message dialog is shown to the user informing them of the find:
In the HTML code below, the form fields are defined. Notice that in the form header we include an
enctype
and set it as multipart/form-data. This is because this form will
also be used to upload files. By declaring the enctype
as multipart/form-data
we automatically create a browse button on our form that enables users to
upload a file from their system: