The first thing that you need to do is figure out where you want the survey. You want to position the poll in the body of your page, but you need it in an area that is visible. If you put it to low on the page, most people will not see it and it may just be a waste of space. The key is to find a spot in the top 1/3rd of the page on either the right or left side. If your site is built with tables or in columns, you can easily implement a small survey. If you do not, then it may take a little finagling to get it to look right. Do not worry about the look of your survey just yet. The style sheets that your site currently employs will make your opinion poll match the rest of the site.
Setting up the database:
Once you have decided where to put it, it is time to figure out where to store the answers. This will involve a small database. Most web hosts will offer at least a limited database support. For these simple quizzes, you can probably park the database for no additional fees. If it does cost you, you may seriously want to consider a new web host. They are probably over charging you for several other things on your site. To set up the database you will need to run a simple SQL statement. This may sound daunting, but it is actually quite simple. You will need to log into your host server. From here you need to figure out how to access your database. There is likely a tutorial on this produced by your host site. They will need to install the database software they use on your account, so you will need to contact them about getting it set up. This is a good time to ask where you would start a new database. If it is the phpMYAdmin screen, this is a self explanatory dashboard that will allow you to click your way through the set up process. If you need to use a SQL statement for this, it may be simpler. The first thing you need to do is select the database that the table is going to be stored in. Now you can create the table "Answers" (or whatever name you want) in the database. Most polls are only a choice of 4 answers, but you may want to create 5 columns in case you want to use five answers in the future. Just copy and paste the following code into your SQL statement builder: CREATE DATABASE SurveyAnswers. This will create the database. You can then copy and paste this code for the table: CREATE TABLE Answers (Poll TEXT(40), A INT(5), B INT(5), C INT(5), D INT(5), E INT(5)).
This may look a little confusing, but it is rather simple. The commands are usually typed in in all upper case. This allows the developers to read the code easier. You are telling the computer to CREATE something. It then needs to know what to create. This is the DATABASE, or the TABLE. When you are creating it, the computer needs to know what to call it. You can not store something without a name. You would never find what you are storing that way. This is the value that immediately follows the TABLE or DATABASE command. The database is always just a blank database. There is nothing else to add to this statement. The tables get a little more complex though. You will need to connect to the database that you are creating in to make the tables. After you tell the SQL that you are creating the table, you will need to define the fields that it creates. The first field you will create will store the name of the quiz. This is the Poll field. It is a TEXT field that will hold up to 40 characters. The rest of the fields are the choices. They are named by the value in the poll. By using this set up, you will need to note the selections you offered for each poll. Another option is to add a couple fields. You can use this set up to hold the answers as well: CREATE TABLE Answers (Poll TEXT(40), ValueA Text(40), A INT(5), ValueB TEXT(40), B INT(5),...
Create a form for your page:
For the rest of this tutorial, you will need to reference the image. I tried to include the code in the article, but it keeps being read as actual html and is not visible to the reader. The code is all in the image, and will just be explained in this article. Sorry for any inconvenience.
Once you have the tables and database set up, you can create the form that will display the survey. This is easily edited so that you can use the same code for every one of your surveys. Since you are adding this to your existing site, the basic tags are already there. All you will need to worry about is the form itself. This is very simple. First you need to create the form tags. This tells your browser where a form named Poll starts and ends. The method and action parts will tell the browser what to do when the submit button is clicked. You will need to ask your host if they use get or post. This is the option that you will use in method. These perform the same function, but some servers are set to use get and some are set for post. The action part will call the php file that we will create later. You just have to make sure the filenames match on these. If you forget either of these tags, it will not look right and may alter the way your page appears.
Now you will create the actual form. After the open tag, you will type your question. You don't need any quotes or anything. Just type your question the way you want it to appear to your visitors. After the question, type in a line break tag. This will move to the next line. Now you can add the radio group. This is a group of selections where only one can be selected and they have the little round circles that you check for your answer. Don't worry, this is another easy step in the process. The nest line of code will create the button and the label that tells the user what each button is for. This code is simple when you break it down and easy to change. The name value is the name of the group of buttons they can choose from. The type just tells the browser to display the radio style of button. Value is an important one, and for this example, it will tell the computer where to store the selection that your visitor has made. It needs to be the same as your database table. We will use this value to select the field that we are adding their vote to. After you close this part, you will need to type the text that you want to appear for the selection. It is important to close the input after the selection is entered. By adding the line break (/br) after the input, it will go to the next line so that your choices are in a list form. If you want them to appear in one straight line, just leave this tag out. You will need to repeat this line of code for each selection. As long as the name value stays the same, they will only be able to choose one of the answers.
After the question and answers are inputted in the form, all that is left is the Submit button. This is another simple function. For this button, the closing tag is included, so you will not need to add that. The type value tells the browser that it is a submit button and it should do what the action value in the form tag tells it to do. The name is optional since we are not calling it in any other sections. The value attribute is what text you want to appear on the button itself. That is it. The closing form tag should be after all of this code.
A dash of PHP:
Now that you have the form and everything set up, you can add the php script to the mix. This will be a small file that you will put in the same folder on your server as the page. There is no need for any compiler either. You can use any basic text editor like Notepad for this. All you need to do is set it up to write the value to your database. All php files start with ?php and end with ?. Everything you want coded in the file should fall between these two tags. The first thing we need to do is open the database connection. To do this, you will need to tell the browser some information about your database. To set up the variables, just type these on separate lines: $host = "localhost"; $user = "*********"; $pass = "*********"; $db = "SurveyAnswers";. Each dollar sign is the start of a new line. The $host is where your database is located. It is usually local host, but can be different. You will want to ask your web server people for this info. The $user and $pass are the user name and password that you use to access your database. This is the reason the php file is in a separate location from the page. You can put the php code in the web page in the header section, but then anyone can access your password. Having a separate location is safer.
Coding the beast:
Now you can connect to the actual server and pass information to and from the database. "$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect! We apologize for any inconvenience, but the database is currently malfunctioning. Please try again later.");" goes on the next line and followed by: "mysql_select_db($db) or die ("Unable to select database! We apologize for any inconvenience, but the database is currently malfunctioning. Please try again later.");" on another, minus the quotation marks. Now you are connected. The update will be a simple SQL statement. First you will need to know which field to update. For this you will need to get the answer the user submitted. Create a new variable called Answer and assign the value to it. $Answer = $_GET['Result']; is all you need for that. If you remember, Result was the name of our radio group from the form. This tells the browser to grab the value of the checked answer. Now you can select that field and add one to it. $ResultSQL = "SELECT '$Answer' FROM Answers"; will set the SQL statement. To run that statement you will have to use this on the next line: $Result = mysql_query($ResultSQL);. Now the $result has the number of times that answer has been checked by all users. To add one: $Result = $Result + 1;. Now you can send the value back to the database with another query. $UpdateSQL = "UPDATE Answers SET '$Answer' = ' $Result'";. This tells the database to update the Answers table where the field equals the variable Answer (the choice the user gave) with the value in Results (the old value plus one). After you run this query, all that is left is to close the connection with mysql_close; and you are done.
It may sound like a daunting task, but the setup is the hardest part. Once all of his is set up, you can easily change the values and offer a new opinion poll every week. This will drive traffic to your site with new keywords and bot visits. For about 30 minutes of work, you could get countless new hits to your site, causing a surge in revenue from ads or sales.
Published by RH
- Struggling to Increase Site Traffic? Do These 3 Simple and Effective Methods to Su...
- How to Install an Advanced Poll Through Your C-Panel
- How to Pass All of the Elements of a Form to a Second Form with PHP
- Looking to Boost Your Site Traffic Generation? Be Sure You Do These 3 Simple Thing...
- How to Rent Out Your Vacation Home
- How to Read and Understand a Topographic Map
- Converting Text to HTML with PHP




