0

I'm developing a kind of simple form for a side project I am doing.

It's based on a very simple contact form.

I have a very similar one working here

https://crgdev.com/Contact/

But the one here doesn't appear to be working

https://crgdev.com/BrandAddition/

Code below (very new to the site so probably doing something wrong)


<?php

if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED

    $email_to = "chris@crgdev.com";
    $email_subject = "New Starter - ";


    function died($error) {

        // your error code can go here

        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }


    // validation expected data exists

    if(!isset($_POST['first_name']) ||
       !isset($_POST['last_name']) ||
       !isset($_POST['email']) ||
       !isset($_POST['accessrequirements'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');
    }

    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $accessrequirements = $_POST['accessrequirements']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

    if(!preg_match($email_exp,$email_from)) {
        $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
    }

    $string_exp = "/^[A-Za-z .'-]+$/";

    if(!preg_match($string_exp,$first_name)) {
        $error_message .= 'The First Name you entered does not appear to be valid.<br />';
    }

    if(!preg_match($string_exp,$last_name)) {
        $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
    }

    if(strlen($accessrequirements) < 2) {
        $error_message .= 'The Comments you entered do not appear to be valid.<br      />';
    }

    if(strlen($error_message) > 0) {
        died($error_message);
    }

    $email_message = "Form details below.\n\n";


    function clean_string($string) {
        $bad = array("content-type","bcc:","to:","cc:","href");
        return str_replace($bad,"",$string);
    }


    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Start Date: ".clean_string($start_date)."\n";
    $email_message .= "Job Title: ".clean_string($job_title)."\n";
    $email_message .= "Team Leader: ".clean_string($team_leader)."\n";
    $email_message .= "Location: ".clean_string($location)."\n";
    $email_message .= "Access Requirements: ".clean_string($accessrequirements)."\n";
    $email_message .= "Email/Phone Access: ".clean_string($emailaccess)."\n";
    $email_message .= "Other Requirements: ".clean_string($otherrequirements)."\n";
    $email_message .= "Subitted by: ".clean_string($email_from)."\n";


    // create email headers

    $headers = 'From: '.$email_from."\r\n".
               'Reply-To: '.$email_from."\r\n" .
               'X-Mailer: PHP/' . phpversion();

     @mail($email_to, $email_subject, $email_message, $headers);

?>



 <!-- include your own success html here -->



Thank you for submitting your New STarter Form. We will be in touch with you    regarding any updates.

<html>
<p>Go back to <a href="https://brandaddition.com">Brand Addition/a></p>
</html>

<style>
body {
    font-size: larger;
    text-align: center;
 }
</style>


<?php

}

?>

<body>
<div class="fixedwidth">

<div id="brandlogo"><img src="brandadditionlogo.png"</div>

<h1>New Starter Form</h1>
<div id="formdiv">
<form name="newstarterform" method="post" action="send_form_email.php">
<table id="formtable">
 <tr>
   <td><input  type="text" name="first_name" placeholder="First Name" maxlength="50"></td>
 </tr>
 <tr>
   <td><input  type="text" name="last_name" placeholder="Last Name" maxlength="50"></td>
 </tr>
 <tr>
    <td>Start Date: <input type="date" name="start_date" placeholder="Start Date" maxlength="50"></td>
 </tr>
 <tr>
    <td><input type="text" name="job_title" placeholder="Job Title" maxlength="50"></td>
 </tr>
 <tr>
    <td><input type="text" name="team_leader" placeholder="Team Leader" maxlength="50"></td>
 </tr>
 <tr>
    <td> Location:
        <select name="location">
        <option value="Manchester">Manchester</option>
        <option value="London">London</option>
        <option value="Ireland">Ireland</option>
        </select>
    </td>
 </tr>
 <tr>
   <td>
    <p class="pagepara"><span class="bold">List the access requirements needed, this should include</span><br><br>
      • Level of oasis access or which user we should copy to set the oasis access level<br>
      • Corporate programs that the user should have access to<br>
      • Any specific applications that the employee would need access to<br>
      • Which share drive folders should the employee have access to<br>
    </p>
    <textarea name="accessrequirements" placeholder="Access Requirements" maxlength="1000" cols="50" rows="10"></textarea>
   </td>
 </tr>
 <tr>
   <td>
    <p class="pagepara"><span class="bold">List the email and telephone groups that the user should receive<br> communications from</span><br><br>
    </p>
    <textarea name="emailaccess" placeholder="Email/Phone Access" maxlength="1000" cols="50" rows="10"></textarea>
   </td>
 </tr>
 <tr>
   <td>
     <input type="checkbox" name="desktop computer">Desktop Computer
     <input type="checkbox" name="laptop">Laptop <br>
     <input type="checkbox" name="mobile phone">Mobile phone
     <input type="checkbox" name="remote access">Remote Access<br><br>
     <textarea name="otherrequirements" placeholder="Other requirements not mentioned above?" maxlength="1000" cols="50" rows="10"></textarea>
   </td>
 </tr>
 <tr>
   <td>
     <p class="bold">Enter your email below for communications</p>
     <input type="email" name="submitter">
   </td>
 </tr>
 <tr>
   <td style="padding: 40px 0px 80px 0px">
     <input type="submit" value="Submit">
   </td>
  </tr>
  </table>
  </form>
</div>
 <div id="blueline"></div>
</div>
</body>
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
  • For one, in your code, where do you send the POST requst "email" as set in your PHP file? I never found a send request with the name equivelant to "email." The `isset` is probably your problem if "email" is not being sent to the PHP file. –  Mar 08 '17 at 23:48
  • died()? I think you mean die() http://php.net/manual/en/function.die.php AND ALWAYS enable error display when writing a new script, `error_reporting(E_ALL); ini_set('display_errors', 1); ` – Duane Lortie Mar 09 '17 at 00:05

1 Answers1

0

I can't guarantee this is the only problem with your site. However, I can guarantee this may solve a problem or two.

In your PHP code, you have an if statement that determines if $_POST["email"]; exists, right?
I went over your HTML document and found nothing in <form> with the attribute name="email". How would that transfer over to your PHP document? It returns nothing because the page basically dies. I'm not sure about the "success HTML," but it may have something to do with the out of place end bracket for your if statement that I requested you to change.

We can test this theory if you add an else statement after the end of the if statement, and also make sure that you're submitting an input or something with the name attribute set to "email."


**EDIT:**On your email textbox, your name attribute is set to "submitter." Change it to "email," then fix up your code so it will actually work. This will at least give you some feedback.