0

I have a question regarding a form I made.

It's a big form and what I want is that I receive all the input in my database (I have accomplished this so far) and that I get an email including all the results from my form. Eventually what I want is a neat stylized email that provides the complete question and the answer filled in by the client.

In short: I want all the questions and answers from my form put together in an email.

<div class="formulier">
  <form method="post" enctype="multipart/form-data" action="action.php"> 
    <div id="alginfo">
      <div class="col-md-12 red">
        <div class="container">
          <h3 class="numbers">1</h3>
          <h3 class="title">Algemene informatie</h3>
        </div>
      </div>
      <div class="container">
        <div class="col-md-6">
          <div class="tekstenveld">
            <input type="text" class="bigv bedrijfsnaam" name="bedrijfsnaam" required>
            <p>Bedrijfsnaam</p>
          </div>

          <div class="tekstenveld">
            <input type="text" class="bigv volledigenaam" name="volledigenaam" required>
            <p>Volledige naam</p>
          </div>
        </div>

        <div class="col-md-6">
          <div class="tekstenveld">
            <input type="text" class="bigv telefoonnummer" name="telefoonnummer" required>
            <p>Telefoonnummer</p>
          </div>

          <div class="tekstenveld">
            <input type="text" class="bigv email" name="email" required>
            <p>E-mail</p>
          </div>
        </div>

        <div class="col-md-6">
          <div class="multikeuze">
            <h4>Betreft het een:</h4>
            <ul>
              <li><span class="error"></span>
              <input class="multikeuzes" type="checkbox" name="website" id="website" value="website">
              <label for="website" name="website">Website</label></li>
              <li><input type="checkbox" name="webshop" id="webshop" value="webshop">
              <label for="website" name="webshop">Webshop</label></li>
              <li><input type="checkbox" name="app" id="app" value="app">
              <label for="website" name="app">App</label></li>
              <li><input type="checkbox" name="onlinemarketing" id="onlinemarketing" value="onlinemarketing">
              <label for="website" name="onlinemarketing">Online marketing</label></li>
            </ul>
            <p>Anders</p>
            <input type="text" class="bigv anders" name="alginfoanders">
          </div>
        </div>
      </div>
    </div>
   </div>
<div class="col-md-12 text-center" id="finish">
      <input type="submit" name="submit" value="submit">
    </div>
</form>

So that's my html, it's only a small part of the form because including everything would make it unnecessarily big. Now here comes my action.php it is also shortened so I hope you can still understand what's going on.

<?php
$servername = "youdliketoknowthat.com";
$username = "butitssecret";
$password = "hunter123";
$dbname = "yougettheidea";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

if(isset($_POST['submit'])){
$to = '[email protected]';

$subject = 'Content formulier';

$headers = "From: [email protected]\r\n";

$message = 'Bedrijfsnaam: ' . $bedrijfsnaam;
$message .= 'Volledige naam: ' . $volledigenaam;
$message .= 'Telefoonnummer: ' . $telefoonnummer;
$message .= 'email: ' . $email;

mail($to, $subject, $message, $headers);

$bedrijfsnaam = $_POST['bedrijfsnaam'];
$volledigenaam = $_POST['volledigenaam'];
$telefoonnummer = $_POST['telefoonnummer'];
$email = $_POST['email'];
if($volledigenaam !=''||$email !='');
$website = $_POST['website'];
$webshop = $_POST['webshop'];
$app = $_POST['app'];
$onlinemarketing = $_POST['onlinemarketing'];

{

$sql = "INSERT INTO intake_formulier_test (bedrijfsnaam, volledigenaam, telefoonnummer, email, website, webshop, app, onlinemarketing)
VALUES ('$bedrijfsnaam', '$volledigenaam', '$telefoonnummer', '$email', '$website', '$webshop', '$app', '$onlinemarketing')";
}
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
}

$conn->close();
?>

I hope I made it clear what I am trying to accomplish. I feel like this should be so easy yet I can't find an answer on Google that applies to what I am trying to do.

3
  • So what ? Is there a problem or something ? Commented Jan 29, 2016 at 10:10
  • I'm voting to close this question as off-topic because it belongs to codereview.stackexchange.com Commented Jan 29, 2016 at 11:37
  • 1
    @Toto This question is about a specific programming issue (my code is supposed to send me a summary by email, it doesn't). How on earth is that not suitable here? Even though it wasn't, this question is off-topic for codereview as it contains broken code. Commented Jan 29, 2016 at 11:49

1 Answer 1

1

In short:

Dont put this :

$message = 'Bedrijfsnaam: ' . $bedrijfsnaam;

Before this:

$bedrijfsnaam = $_POST['bedrijfsnaam'];

Same things for all others variables used for your mail() function

Your php script should be like this:

<?php
$servername = "youdliketoknowthat.com";
$username = "butitssecret";
$password = "hunter123";
$dbname = "yougettheidea";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

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

$bedrijfsnaam = $_POST['bedrijfsnaam'];
$volledigenaam = $_POST['volledigenaam'];
$telefoonnummer = $_POST['telefoonnummer'];
$email = $_POST['email'];
if($volledigenaam !=''||$email !='');
$website = $_POST['website'];
$webshop = $_POST['webshop'];
$app = $_POST['app'];
$onlinemarketing = $_POST['onlinemarketing'];

{

$sql = "INSERT INTO intake_formulier_test (bedrijfsnaam, volledigenaam, telefoonnummer, email, website, webshop, app, onlinemarketing)
VALUES ('$bedrijfsnaam', '$volledigenaam', '$telefoonnummer', '$email', '$website', '$webshop', '$app', '$onlinemarketing')";
}
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
    // Now we can send email
    $to = '[email protected]';
    $subject = 'Content formulier';
    $headers = "From: [email protected]\r\n";
    $message = 'Bedrijfsnaam: ' . $bedrijfsnaam;
    $message .= 'Volledige naam: ' . $volledigenaam;
    $message .= 'Telefoonnummer: ' . $telefoonnummer;
    $message .= 'email: ' . $email;
    mail($to, $subject, $message, $headers);
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
}

$conn->close();
?>

You were adding things to your $message var like this: $message = 'Bedrijfsnaam: ' . $bedrijfsnaam; But at that point, for this particular exemple, the var $bedrijfsnaam wasn't declared... So your $message var was empty ! In my example, i put all the things related to emailing infos after DB query, and after all your $var = $_POST["var"]..

Sign up to request clarification or add additional context in comments.

1 Comment

Tip: For a "neat stylized email" you can use html and css in your $message, for that, read Usman Khan answer ! ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.