Create a static webpage with a contact form on Github pages
Tags: webdev, github, beginners
Published: 2020-05-18 17:10:58

## What are GitHub pages?
GitHub pages allow you to host a website directly from your GitHub repository. It can host static websites that take HTML, CSS, and JavaScript files straight from your GitHub repository. It cannot execute server-side code like PHP so we can not add a contact form that redirects to a PHP file. That is where formspree comes in.

## Steps

#### Step 1: Create a new repository named `<username>.github.io`. `<username>` is your GitHub username.


<img src="https://dev-to-uploads.s3.amazonaws.com/i/j7wukzt66oncohyemk53.PNG" width="500">

<b> <u> Note:</u></b> My repo name shows an error since I already have a repository named `CharalambosIoannou.github.io`. Also, the repository must be set to public. 


#### Step 2: Create your static page. Make sure the main html file is named `index.html`.

In my case I have created my portfolio website as can be seen from the image below. 


<img src="https://dev-to-uploads.s3.amazonaws.com/i/1zmfgp5r1xikmbtsiwnh.PNG" width="500">

#### Step 3: Navigate to [formspree](https://formspree.io/)

1) In [formspree](https://formspree.io/) click the sign-up button in the top right corner and enter your email address along with a password.
2) Verify your email
3) Press the `+` button and enter a form name. The email input box should be auto filled with the email address used to sign up to fromspree.




<img src="https://dev-to-uploads.s3.amazonaws.com/i/hztdnbp1wsfkh2y2c4d6.PNG" width="400">



4) You can then copy paste the code provided by formspree into your website.



<img src="https://dev-to-uploads.s3.amazonaws.com/i/yqi2ns67v9vgl2wyjsfd.png" width="1000x500">


I have altered this code slightly to allow visitors to add a message as well. The code used is:
```html
<div id="contact">
        <h2>Get in Touch</h2>
        <div id="contact-form">
                <form action="https://formspree.io/mpzyqdng" method="POST">
                <input type="hidden" name="_subject" value="Contact request from personal website" />
                <input type="email" name="_replyto" placeholder="Your email" required>
                <textarea name="message" placeholder="Type your message" required></textarea>
                <button type="submit">Send</button>
            </form>
        </div>
    </div>
```

And this code produces the following result:

<img src="https://dev-to-uploads.s3.amazonaws.com/i/ws1q3shlwucz5eql7tez.PNG" width="500">



#### Step 4: Push your code to GitHub and navigate to your website by typing `https://<username>.github.io/` in the URL.

If you cannot see your website after some time has passed then go to your repository, click on settings, scroll down until you see a section named "GitHub Pages" and click on the None dropdown in the Source subsection and set it to master branch.

You know if your website is published if you see this message in the "GitHub Pages" section:



<img src="https://dev-to-uploads.s3.amazonaws.com/i/76rio32cdzc0ykdwmcao.PNG" width="500">


#### Step 5: Testing your form.

If you want to test your form, then go to your website and fill out your form.
When clicking the send button we get redirected and have to authenticate that we are not a robot :robot:. When ticking the box, soon enough you will receive an email with all the details the sender has provided.

![Final Step](https://dev-to-uploads.s3.amazonaws.com/i/ctyg7omwkioahgz7hst6.PNG)

## THAT’S IT!!! 

I hope you find this easy and useful.

P/S: This is my first post ever. Hope you enjoyed it :smile:.

Happy Exploring!!

Resources: GitHub