Link to home
Start Free TrialLog in
Avatar of 1524
1524

asked on

competition form with php/mysql?

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 :)

Avatar of axis_img
axis_img

Hello...

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.

main.php
*****************

<html>
<head>
<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   = "http://www.you.com/popup.php";
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 = window.open(popup_url, "popup", "width=" + popup_width + ",height=" + popup_height);
}

//-->
</script>

</head>

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


popup.php
*******************

<?
// 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, "/");
                                include_once("thank_you.php");

                                // Close DB connection
                                mysql_close($my_conn);
                                exit;
                        }
                }
        }
}
?>
<html>
<head>
<title>Popup Window</title>
</head>

<body>

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

<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">
</form>
</body>
</html>


thank_you.php
**********************

<html>
<head>
<title>Thank You</title>
</head>

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

**************************************


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.


Regards,
Barry
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).

Regards,
Barry
Avatar of 1524

ASKER

Hi Barry, thanks for your help, much appreciated.

here's the result: http://undergroundhouse.net/main_page.php

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!
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.

<META HTTP-EQUIV="Refresh" CONTENT="0;URL=http://undergroundhouse.net/main_page.php">

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.

Regards,
Barry
Avatar of 1524

ASKER

"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.

Cheers


Ben
Hey Ben...

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

# SET DEFAULT INDEX ORDER
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...
Barry
ASKER CERTIFIED SOLUTION
Avatar of axis_img
axis_img

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 1524

ASKER

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 :)
Avatar of 1524

ASKER

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


-


Ben