1

I am attempting to make a website for my school's robotics team. So far, everything is working well, except for the contact form. I know I need to use PHP to make it work, and I found a guide online for how to do it. I copied it exactly, changing details as needed. However, It did not work at all. Below is the Contact Section of the HTML.

                    <!-- Contact -->
                        <article id="contact">
                            <h2 class="major">Contact</h2>
                            <form method="post" action="C:\Users\Mr. Roboto 2\Desktop\Robotics New Website\assets\form-to-email.php">
                                <div class="field half first">
                                    <label for="name">Name</label>
                                    <input type="text" name="name" id="name" />
                                </div>
                                <div class="field half">
                                    <label for="email">Email</label>
                                    <input type="text" name="email" id="email" />
                                </div>
                                <div class="field">
                                    <label for="message">Message</label>
                                    <textarea name="message" id="message" rows="4"></textarea>
                                </div>
                                <ul class="actions">
                                    <li><input type="submit" value="Send Message" class="special" /></li>
                                    <li><input type="reset" value="Reset" /></li>
                                </ul>
                            </form>
                            <ul class="icons">
                                <li><a href="#" class="icon fa-twitter"><span class="label">Twitter</span></a></li>
                                <li><a href="#" class="icon fa-facebook"><span class="label">Facebook</span></a></li>
                                <li><a href="#" class="icon fa-instagram"><span class="label">Instagram</span></a></li>
                                <li><a href="#" class="icon fa-github"><span class="label">GitHub</span></a></li>
                            </ul>
                        </article>

Below is the PHP code I found from the guide.

<?php
if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];

//Validate first
if(empty($name)||empty($visitor_email)) 
{
    echo "Name and email are mandatory!";
    exit;
}

if(IsInjected($visitor_email))
{
    echo "Bad email value!";
    exit;
}

$email_from = 'tom@amazing-designs.com';//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
    "Here is the message:\n $message".

$to = "FRCteam3236@gmail.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');


// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}

?> 

I am a complete newbie to PHP, so none of that makes any sense to me. Whenever I press the button to submit a message, all that happens is the PHP code shows up on a blank white page. I have heard from some people that I need a mail server, but I have no idea how to set one up. Nothing I have found online has helped, so I decided to come to good old StackOverflow for help. Hopefully someone can help me. Any help would be much appreciated.

Edit: Apparently this was marked as a duplicate, when I do not believe it is. The question I supposedly duplicated was not experiencing the same problems as me.

Cubemaster
  • 507
  • 1
  • 6
  • 20
  • if you see the php in the browser, your not even running a php enabled server (fix that first) –  Feb 14 '17 at 20:33
  • 1
    `"the PHP code shows up on a blank white page"` - Then PHP isn't executing at all. Probably because you're calling it from the file system instead of from a web server: `action="C:\Users\Mr. Roboto 2\Desktop\Robotics New Website\assets\form-to-email.php"` PHP runs on a *web server*, not in the browser. – David Feb 14 '17 at 20:33

0 Answers0