Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


competition form with php/mysql?

Posted on 2002-05-19
Medium Priority
Last Modified: 2006-11-17
I've been asked to run a cd giveaway/competition on my site and would like to have a popup box load on the way into my site with some info about the cd, a graphic of the CD cover and a form with fields for Name and Email Address. Once submitted I'd like the user to be shown and "thankyou for entering" screen and then have a "close this window" link and not be shown the popup next time they visit the site (cookies?). I'd also like to store the names and email addresses in a mysql table.

I can do the html bit but I'm still a bit of a noob on the php/mysql side and was wondering if anyone could suggest what code I would need to achieve this.

thanks in advance :)

Question by:1524
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4

Expert Comment

ID: 7020629

I am just going to throw this out here, since your request does not really warrant a simple answer. It is a bit more involved.

First thing to take care of is setting up the database table. I am assuming that you are using mysql. Please let me know.

For this example, I just created a simple table named "cd_submissions", with the fields "user_email" and "user_name".

An example create command would be:

CREATE TABLE cd_submissions (
user_email varchar(255) not null,
user_name varchar(100) not null);

// Prevent duplicate emails
CREATE UNIQUE INDEX cd_submissions_ux1 ON cd_submissions(user_email);

I prefer using unique indexes of primary keys when possible, but that is just a habit I picked up from my use of oracle. You can choose for yourself.

When that is done, you can begin with the files. The first file is the main page, which will launch the popup window.


<title>Main Page</title>

<script language="javascript">
// Do not change
var popup_seen  = <?=(isset($HTTP_COOKIE_VARS["popup_seen"]))?"true":"false";?>;

// Change as needed
var popup_url   = "";
var popup_width = 300;
var popup_height= 300;

function show_popup() {

        // If the cookie is set, do nothing
        if(popup_seen) return;

        // Cookie was not found, show window
        var popup =, "popup", "width=" + popup_width + ",height=" + popup_height);



<body onLoad="show_popup();">
Your main page is here...


// If the form was submitted, act upon it...
if($form_active == 1) {

        // Cookie expiration time [currently 1 year]
        $expire_time    = 86400 * 365;

        // DB configuration values
        $db_host        = "localhost";
        $db_username    = "username";
        $db_password    = "password";
        $db_name        = "database_name";

        // Initialize the error array
        $form_errors = array();

        // Validate name
        if($HTTP_POST_VARS["user_name"] == "") {
                $form_errors["user_name"] = "Please enter your name";

        // Validate email
        if($HTTP_POST_VARS["user_email"] == "") {
                $form_errors["user_email"] = "Please enter your email address";

        // No errors were found, so go ahead and submit into the DB
        if(sizeof($form_errors) == 0) {
                $user_name      = addslashes($user_name);
                $user_email     = addslashes($user_email);

                // Create a connection to your database...
                // Change the values to your environment's specific setup
                if( ($my_conn = mysql_connect($db_host, $db_username, $db_password)) != false) {;

                        // Select database name...
                        mysql_select_db($db_name, $my_conn);

                        // Create query for the insert...
                        $insert_query   = "INSERT INTO cd_submissions VALUES('$user_email', '$user_name')";
                        $insert_result  = mysql_query($insert_query, $my_conn);

                        // If insert worked, include thank you file
                        if($insert_result != false) {

                                // Set cookie so they do not see the popup again
                                setcookie("popup_seen", "1", $expire_time, "/");

                                // Close DB connection
<title>Popup Window</title>


Your info and CD graphics would most likely go here somewhere...

<form action="popup.php" method="post">
<input type="hidden" name="form_active" value="1">

<?=($form_errors["user_name"] != "") ? $form_errors["user_name"] . "<br>":"";?>
Your Name: <input type="text" name="user_name" value="<?=$HTTP_POST_VARS['user_name']?>"><br>

<?=($form_errors["user_email"] != "") ? $form_errors["user_email"] . "<br>":"";?>
Your Email:: <input type="text" name="user_email" value="<?=$HTTP_POST_VARS['user_email']?>"><br>
<input type="submit" value="Submit">


<title>Thank You</title>

<b>Thank you</b>
<a href="javascript:;" onClick="window.close(); return false;">Close this window</a>


I am not sure what parts of this you will have questions on, so you let me know as you have problems. I will be on and off tonight, so I will get back as time permits. Good luck.


Expert Comment

ID: 7020633
Just an FYI. I used basic examples in some of the code above. There is not a lot of error checking, etc. For instance, you may want a better email validator. There are plenty floating around on the web, which can be plugged in.

Also, the layout is nasty. It is just simple text. I didn't want to waste time trying to make the html look pretty, since you will obviously need to customize it to your needs.

And finally... I am not sure which version of PHP you are using. If you are using a newer version, you can substitute the $HTTP_POST_VARS with $_POST, and $HTTP_COOKIE_VARS with $_COOKIE. The reason for this is because $HTTP_*_VARS has been deprecated in the newer versions (although they are still functional).


Author Comment

ID: 7021739
Hi Barry, thanks for your help, much appreciated.

here's the result:

I've got everything working apart from I've still got one small problem.

my site has an index.htm which is the index page of the site and an index.php already I can't easily rename the exsiting index.php and It's a hosting account so I can't change the name of the default index page. The new file main_page.php needs to somehow my site index page. any ideas??

thanks in advance!
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Expert Comment

ID: 7021808
Cool! Glad it worked. Looks good, actually... heh.

Hmmm.. Let me make sure I understand the problem fully.

The default directory index page for your site is set up as: index.htm index.php, in that order?

Also, your server doesn't allow .htaccess files to override the directory indexes, or to add .htm as a parsed type?

Bummer :)

Offhand, the only thing I can think of is for you to simply redirect the user from the index.htm page to the main_page.php page using a meta tag. I know it is a bit sloppy, but it is a workaround; at least until you happen to change hosts to one that grants a little more privileges.


Just try sticking that at the top of your index.htm page, so the user is sent to main_page.php instead. Hope that helps out.


Author Comment

ID: 7021819
"Also, your server doesn't allow .htaccess files to override the directory indexes, or to add .htm as
a parsed type?"

actually I didn't know that you could overide the directory indexes.

what code do I need in a .htaccess file to do that? I reckon that could solve the problem, although I'll need to amend my existing htaccess unless I can have more than one.



Expert Comment

ID: 7021846
Hey Ben...

Yeah... you can only have one .htaccess file. Try putting this line in there as well:

DirectoryIndex main_page.php index.php index.html index.htm

Let me know if that works. If it doesn't, it either means there is an error in the htaccess file, or they do not allow overriding this feature (which would be strange, as it is harmless).

Let me know...

Accepted Solution

axis_img earned 1200 total points
ID: 7021852
Oops... "Yeah... you can only have one .htaccess file"

I meant "per directory". It sounded a bit confusing when I re-read it. :)

Author Comment

ID: 7021903
I did exactly what you said and it still doesn't work, but I guess I should probably speak to my hosting guys now. Thanks for all your help, much appreciated :)

Author Comment

ID: 7392712
Hi Barry, just a quick question for you on the subject of this popup code... I can post this as another question if you like...

I'm changing this for a new competition each month now, but I figured that people who entered the comp the month before, won't be seeing the popup for the new comp, unless they have deleted the cookie... any ideas how to work around this... I'm just about to put a new comp live now...

thanks in advance



Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question