How to Code PHP: Activate Account & Multiple Drop Down Scripts

Here I’ll show you the code that will allow a new user to activate their account and how to create multiple drop down boxes that automatically update. This should be a wild ride so let’s learn How to Code PHP.

PHP Activate Account Script

Obviously, you are going to need to connect to the database and include all of the HTML needed. See my first How to Code PHP Tutorial for that information. I’m just going to include the PHP code for the activation script to keep the article brief.

Here I’ll show you the code that was used on the New User Registration page. This code created the account and then sent the email, that the user will click to get to your activation page.

$accesscode = md5(uniqid(rand(), true));

The above functions are used to generate a random value, that the new user must have to activate their account. I describe what all of these functions do in the first How to Code PHP Tutorial. In essence these functions create a 32 digit random number.

$query = “INSERT INTO users (email, pass, first_name, last_name, active, registration_date, street, city, state, zip, work_phone, company) VALUES (‘$e’, SHA(‘$p’), ‘$fn’, ‘$ln’, ‘$a’, NOW(), ‘$s’, ‘$c’, ‘$st’, ‘$z’, ‘$ph’, ‘$cmp’)”;

Here we create a query that inserts all of the information into the database.

$result = mysql_query ($query) or trigger_error(“Query: $query\n<br />MySQL Error: ” . mysql_error());

The function mysql_query submits the query to the MySQL server for us. If their is a problem an error is triggered and a message is printed on screen.

if (mysql_affected_rows() == 1) {

Here I’m testing that a row of the data has been added to the database.

$body = “Thank you for registering at the User Registration New Think Tank site. To activate your account, please click on this link:\n\n”;

$body .= “” . mysql_insert_id() . “&y=$accesscode”;

Here I’m creating what will be the string of text that goes in the body of the email we are sending the new customer. We are placing a link to our activation page in the body of that message. The function mysql_insert_id(), will return the primary key for the last row accessed in the database.

mail($_POST[’email’], ‘Registration Confirmation’, $body, ‘From:’);

The mail() function sends the email to the new customer. When the customer gets the email they will click the link inside and have their account activated by the following code.


// Validate $_GET[‘x’] and $_GET[‘y’].

if (isset($_GET[‘x’])) {

$x = (int) $_GET[‘x’];

Remember x is the Primary Key id for the customers new account. Here we are accepting the values x and y, and checking that they have both been set with a value. If they have we are forcing the value of x to be of type int, with this code: (int) $_GET[‘x’];

} else {

$x = 0;


If the variable x wasn’t set, we assign it the value of zero.

if (isset($_GET[‘y’])) {

$y = $_GET[‘y’];

} else {

$y = 0;


We perform the same actions on variable y as we did with variable x.

// If $x and $y aren’t correct, redirect the user.

if ( ($x > 0) && (strlen($y) == 32)) {

We are checking that the values passed are accurate by checking if the Primary Key ($x) is positive. Because all primary keys have to be. Then we are checking that the passcode ($y) is 32 characters in length, because we know that is the length of the number we set.

$query = “UPDATE users SET active=NULL WHERE (user_id=$x AND active='” . escape_data($y) . “‘) LIMIT 1”;

$result = mysql_query ($query) or trigger_error(“Query: $query\n<br />MySQL Error: ” . mysql_error());

We create the query that will change the value of the SQL column named active, if the passcode matches the passcode of the primary key we were sent.

The function escape_data() strips out extra slashes and white space.

if (mysql_affected_rows() == 1) {

echo “<br><br><h3>Your account is now active. You may now log in.</h3>”;

} else {

echo ‘<br><br><p><font color=”red” size=”+1″>Your account could not be activated. Please re-check the link or contact the system administrator.</font></p>’;


If the query was successful, we print a message alerting the visitor to that fact. If not we print an error message.

} else { // Redirect.

// Start defining the URL.

$url = ‘http://’ . $_SERVER[‘HTTP_HOST’] . dirname($_SERVER[‘PHP_SELF’]);

// Check for a trailing slash.

if ((substr($url, -1) == ‘/’) OR (substr($url, -1) == ‘\\’) ) {

$url = substr ($url, 0, -1); // Chop off the slash.


// Add the page.

$url .= ‘/index.php’;

header(“Location: $url”);

// exit(); // Quit the script.

} // End of main IF-ELSE.


Here we redirect the visitor to the index.php, or home page of the site. That is all that goes into creating a user activation page.

Automatically Updating Multiple Drop Down Boxes

PHP Drop Down Menu 1

Here I’ll show you the code behind a semi-complicated task in which we feed new options into a drop down box based off of the selection made in another drop down box. You can see what I’m talking about in the images that follow.

First I’ll use some JavaScript Scripts to reload the form whenever the visitor changes the values of the drop down box.

PHP Drop Down Menu 2

<script language=JavaScript>

function reload(form)



self.location=’index.php?cat=’ + val ;


function reload3(form)



var val2=form.subcat.options[form.subcat.options.selectedIndex].value;

self.location=’index.php?cat=’ + val + ‘&cat3=’ + val2 ;



The function reload and reload3 are called when the user changes the value of the drop down in the form below. These functions send the visitor to the index.php page with the new values for the drop down boxes attached to the url. If this doesn’t make sense read my JavaScript Scripting Tutorial.

Here is the PHP code that will pull new drop down box information from the database.

$quer2=mysql_query(“SELECT DISTINCT Prod_Type,PT_ID FROM Product_ID

WHERE ((PT_ID = 0) OR (PT_ID = 1) OR (PT_ID >= 2 AND PT_ID <= 4) OR (PT_ID >= 7 AND PT_ID <= 8) OR (PT_ID >= 14 AND PT_ID <=16) OR (PT_ID >=31 AND PT_ID <= 37)) order by PT_ID”);

Here I’m creating the options for the first drop down box, by making a query. If you read my SQL Statements Tutorial, this should all make perfect sense. That is all the coding you have to perform to populate the first drop down.

Now I’ll make the second drop down, based off of the option chosen in the first drop down.


if(isset($cat) and strlen($cat) > 0){

Here we’re checking that a value has been set for the first drop down box.

$quer=mysql_query(“SELECT DISTINCT Manufacturer_ID.Manufacturer, Manufacturer_ID.Man_ID FROM Manufacturer_ID, Model_Numbers where ((Manufacturer_ID.Man_ID = Model_Numbers.Man_ID) AND (Model_Numbers.PT_ID = $cat)) order by Manufacturer”);

}else{$quer=mysql_query(“SELECT DISTINCT Manufacturer,Manufacturer_ID FROM Manufacturer_ID order by Manufacturer”); }

We create a SQL query that returns the Manufacturer ID and Manufacturer Name where the ID numbers match and the Part ID is equal to the value the visitor choose in the first drop down. We then alphabetically order these results.

If no value is set for the first drop down box, we populate the second drop down with every manufacturer in the database. This is all the code needed for the second drop down

$cat3=$_GET[‘cat3’]; // This line is added to take care if your global variable is off

if(isset($cat3) and strlen($cat3) > 0){

$quer3=mysql_query(“SELECT DISTINCT Model_Num, Model_Num FROM Model_Numbers where ((Model_Numbers.Man_ID=$cat3) AND (Model_Numbers.PT_ID = $cat)) order by Model_Num”);

}else{$quer3=mysql_query(“SELECT DISTINCT subcat2 FROM subcategory2 order by subcat2”); }

Here we perform very similar actions with the third drop down box. We are performing a query that will return the Model Numbers when the Manufacturer ID matches $cat3 and Model Numbers match the value of $cat.

Now we need some PHP code to generate the form that contains our drop downs.

echo “<select name=’cat’ onchange=\”reload(this.form)\”><option value=”>Select one</option>”;

while($noticia2 = mysql_fetch_array($quer2)) {

if($noticia2[‘PT_ID’]==@$cat){echo “<option selected value=’$noticia2[PT_ID]’>$noticia2[Prod_Type]</option>”.”<BR>”;}

else{echo “<option value=’$noticia2[PT_ID]’>$noticia2[Prod_Type]</option>”;}


echo “</select>”;

This code creates a drop down that will reload the page each time a drop down is changed. This is done with some help from the onchange event handler.

The while loop will loop through all of the query data that is returned with some help from the mysql_fetch_array() function.

The mysql_fetch_array() function fetches a row of data at a time, from the array created from the query above.

The rest of the code prints out the rest of the HTML code for the drop down box. The rest of the code creates the next two drop down boxes and is nearly identical to the above.

echo “<select name=’subcat’ onchange=\”reload3(this.form)\”><option value=”>Select one</option>”;

while($noticia = mysql_fetch_array($quer)) {

if($noticia[‘Man_ID’]==@$cat3){echo “<option selected value=’$noticia[Man_ID]’>$noticia[Manufacturer]</option>”.”<BR>”;}

else{echo “<option value=’$noticia[Man_ID]’>$noticia[Manufacturer]</option>”;}


echo “</select>”;

echo “<select name=’subcat3′ ><option value=”>Select one</option>”;

while($noticia = mysql_fetch_array($quer3)) {

echo “<option value=’$noticia[Model_Num]’>$noticia[Model_Num]</option>”;


echo “</select>”;


<input type=submit name=Submit value=Submit>


That’s All Folks

You are now a pretty good PHP / SQL Programmer. I think I’ve covered some of the most complicated tasks you can accomplish with PHP. If you have any questions leave them below.

Till Next Time…

Think Tank

10 Responses to “How to Code PHP: Activate Account & Multiple Drop Down Scripts”

  1. swati says:

    i’ve seen this code on and used it successfully but what happens if you need to use the data of subcat to populate another field in the same form.
    ex. : you choose state (DD1), city(DD2). then you need to use the value of DD2 to select locality which comes at the bottom of the form – for a variety of reasons, it cannot come up as the DD3 when processing cat and subcat. do u have a suggestion for that ?

    • admin says:

      I’m not sure I understand your question? Do you want to know how to change the value of the third drop down, when the first is changed? If your having a problem implementing the code, I would guess it has to do with any query change’s you’d have to make because your database would be different. If you could provide more information I’d be very happy to help.

  2. sam says:

    hello could do all this in a video tutorial
    “Automatically Updating Multiple Drop Down Boxes”

  3. Aknikk says:

    how to create the comment box could you mail me the codes.. plz and how to make a login page , and profile page. i have a project plz help me and mail me the info..

  4. Anonymous says:

    I am very happy to see this code but am finding it difficult to use it. I am having four tables in my database (wrinkle, standard, premium and aluminium). I want the first drop down to be able to select those tables. The second, i would like to select the items(fieldname) of either of the tables selected. Then the third, i would want to select the price(fieldnames); with the price, i have 0.4mm, 0.45mm and 0.5m also as (fieldnames). Please can you help me with the code. Thanks.

  5. RC Bruder says:

    Dear Derek,

    I’m Wishing you a quick recovery.

    Your presentations are simply awesome. I am indulged by your clarity and special gift for teaching.

    I am going through all your lessons on PHP programming and I am fascinated by the progress I am obtaining from your tutorials.

    I would like to understand how you are addressing the activation of the user account. The “users” table has the userid (which is the unique code assigned by the user during the registration process). But you are passing the mysql_insert_id(), which was created during the INSERT, in the activation email but not added to the table.

    During the activation process (activate.php), you suggest updating the user table using both the “user_id” (not userid – maybe because you do not want to expose that information through the $_GET), and the random 32 bytes activation code that was inserted in the table. So, my question is, should I have the “user_id” defined as a primary key in the users table and then, make the “userid” (assigned by the user) as a secondary unique key? However, the mysql_insert_id() was not inserted in the table. So, I was a little bit lost as how the id could be used for the activation process.

    I would appreciate your clarification.

    Congratulations again for your wonderful job.

    RC Bruder

    • Derek Banas says:

      Hi RC,

      Thank you 🙂 I’m very happy that you are enjoying them. I actually made an updated login script with a video here PHP Login Script. I also plan on updating everything soon using Zend. It is much more secure. I hope to get that video up this month. You’ll find it very easy to use.

Leave a Reply

Your email address will not be published.