Tag Archives: Form


Tutorial – Add a User Agreement Message to your WordPress

March 14th, 2011 by

This will allow you to require your visitors to accept your User Agreement Terms with this little bit of code. This is great if you want to put a disclaimer, copyright notice, age required notice, or even adult content warning. The code is fairly simple and easy to implement, you will be adding it to your loop file(s) in your theme. Instead of showing you the code in one snippet I will be breaking it up into two because there is a part that goes at the top of the loop and the other part will go at the bottom.

Put this at the very top of your loop file(s)

  1. <?php
  2. session_start();
  3. if(!isset($_SESSION['grant_access'])) {
  4.         $_SESSION['grant_access'] = ‘unset’;
  5. }
  6. if(isset($_POST['agree'])) {
  7.         $_SESSION['grant_access'] = ‘set’;
  8. }
  9. if($_SESSION['grant_access'] == ‘unset’) {
  10. ?>
  11.         <h1>You don’t have Access to this content.</h1>
  12.         <h2>You must click the I Agree button to view this content.</h2>
  13.         <form method="post">
  14.                 <input type="submit" value="I Agree" name="agree" />
  15.         </form>
  16. <?php
  17. } else {
  18. ?>

Put this at the very bottom of your loop file(s)

  1. <?php } ?>

Here is what a very basic loop.php file would look like with the added code.

  1. <?php
  2. session_start();
  3. if(!isset($_SESSION['grant_access'])) {
  4.         $_SESSION['grant_access'] = ‘unset’;
  5. }
  6. if(isset($_POST['agree'])) {
  7.         $_SESSION['grant_access'] = ‘set’;
  8. }
  9. if($_SESSION['grant_access'] == ‘unset’) {
  10. ?>
  11.         <h1>You don’t have Access to this content.</h1>
  12.         <h2>You must click the I Agree button to view this content.</h2>
  13.         <form method="post">
  14.                 <input type="submit" value="I Agree" name="agree" />
  15.         </form>
  16. <?php
  17. } else {
  18. ?>
  19. <?php
  20. if (have_posts()) :
  21.    while (have_posts()) :
  22.       the_post();
  23.       the_content();
  24.    endwhile;
  25. endif;
  26. ?>
  27. <?php } ?>

Now that you have both parts that will be needed lets break it down a little bit further.

  1. <?php
  2. session_start();
  3. if(!isset($_SESSION['grant_access'])) {
  4.         $_SESSION['grant_access'] = ‘unset’;
  5. }

This part of the code is doing two things, first it is telling server to start a session which essentially means it allows your code to use and check for temporarily set cookies. These cookies will only stay on your computer until you close the browser you’re using. This is great for making sure the person who clicked accept is the one viewing your website since once they close the window the cookie will disappear.

The second part is checking if the cookie grant_access has not been set, if it hasn’t then we say it has a value of unset. We need to do this because by default cookies aren’t set until the page is reloaded. On to the next part of the code:

  1. if(isset($_POST['agree'])) {
  2.         $_SESSION['grant_access'] = ‘set’;
  3. }

This is a checking to see if the button “I agree” has been pressed and in turn posted its value. Once the button has been pressed/posted it changes the value of the temp cookie grant_access to set instead of unset. Now onto the third check:

  1. if($_SESSION['grant_access'] == ‘unset’) {
  2. ?>
  3.         <h1>You don’t have Access to this content.</h1>
  4.         <h2>You must click the I Agree button to view this content.</h2>
  5.         <form method="post">
  6.                 <input type="submit" value="I Agree" name="agree" />
  7.         </form>
  8. <?php
  9. } else {
  10. ?>

This part of the code is checking to see if the temp cookie grant_access value is unset. Remember the default value is unset as we defined it above. If it is then it spits out the little bit of HTML. If it value isn’t unset it will show the content as normal. Now for the last bit of the code:

  1. <?php } ?>

This is simply closing the else statement around the code that makes loop function once the user has clicked the I Agree button.

Note: If a user doesn’t have cookies enabled for whatever reason then this message will reappear on every page/post they try to visit as the temporary cookie being set isn’t saving due to cookies being disabled.


Stand Alone PHP Contact Form

March 8th, 2011 by

I wanted to build code for a contact form that could be added to any page with a simple line of code such as this:

  1. <?php include(‘filename.php’); ?>

This version of the form verifies that all fields are filled out. As well as the name field only accepts alpha(a-z) characters, dashes, apostrophes, periods, and spaces. The email field verifies that it follows this standard email format. The phone number field only accepts numbers, spaces, dashes, periods, plus symbols, and parenthesis. The comment field only checks to make sure you put something in it.

The functionality to send the information filled out on the form has been disabled for this demo.


Survey Data Stored to mySQL Database

February 3rd, 2011 by

I was asked to build a survey form that would store the data to a database as well as read the data and convert it to understandable information in a results page. This is a stripped down version of the form that I built for whopeesintheshower.com. It connects with a mySQL database on my servers. It grabs the current integers stored in the respective fields and then increments the respective integer based on which options are filled out on the form.


Various FFXI Calculators in XHTML/PHP

December 29th, 2010 by

Over the course of playing Final Fantasy XI (FFXI) I’ve built various calculators for various formulas I use on a fairly regular basis. I host them online so that others can use them as well. As I come up with more I will just update this page instead of making a new page for each calculator.


XHTML/PHP Auto Responder

December 29th, 2010 by

This auto responder will send both the email notifying owner and the auto response to the same email address. It checks to make sure the name and email are both filled out, as well as make sure the email is a valid email format. After you fill out the form it gives you a thank you message before redirecting you back to the main page.


Tutorial – Delayed Redirect using PHP

December 29th, 2010 by

I had to look into this because I had a form that I wanted to show a thank you message for a moment and then have it redirect back to the main page.

Now the first thought is to use:

  1. header(‘Location: /’);

The problem with this is that as I’m displaying a message beforehand. This becomes an invalid PHP function as it is not first thing being executed.

This is what I found works nicely:

  1. echo ‘<meta http-equiv="refresh" content="3; /" />’;

It is set to wait 3 seconds before redirecting. That is how you can display a message on the page and then have it redirect to another page with a delay so that the visitor has a chance to read the message.

I hope you find this little bit of code useful.