Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP PHP script for e-mail form

Jakobi

Coder
Hi guys,

I'm a total newbie to php and have tried so many things to create a php script that proces my form on my website and sends me an e-mail when people put in ther information..... It's not totally working. Maybe you can help me?


The forms html code is:

HTML:
<div class="row">
      <div class="full">
        <hr class="line30">
        <h2>BESTIL TILBUD VIA FORMULAR</h2>

        <div class="container" style="text-align:left;">
          <form action="kontaktform.php" method="post">

            <label for="name">Dit navn</label>
            <input type="text" id="name" name="name" placeholder="Dit navn..." required>

            <label for="mail">E-mail</label>
            <input type="text" id="email" name="email" placeholder="[email protected]" required>

            <label for="phone">Telefonnummer</label>
            <input type="text" id="phone" name="phone" placeholder="12345678" required>

            <label for="place"><b>Jeg ønsker at...</b></label><br><br>
            <input type="checkbox" id="tilbudillustrationer" name="tilbudillustrationer" value="tilbudillustrationer">
            <label for="tilbudillustrationer" style="margin-right:20px;">Bestille tilbud på illustrationer</label><br><br>
            <input type="checkbox" id="bestilling" name="bestilling" value="bestilling" style="padding:20px;">
            <label for="bestillingsopgave" style="margin-right:20px;">Snakke om en bestillingsopgave</label><br><br>
            <input type="checkbox" id="ide" name="ide" value="ide" style="padding:20px;">
            <label for="ide" style="margin-right:20px;">Jeg ønsker at drøfte en idé</label><br><br>
            <input type="checkbox" id="andet" name="andet" value="andet" style="padding:20px;">
            <label for="andet" style="margin-right:20px;">Andet</label><br><br><br>

            <label for="about">Emne</label>
            <textarea id="about" name="about" placeholder="Min henvendelse drejer sig om..." style="height:200px"></textarea>

            <input type="submit" value="Send">

          </form>
        </div>
      </div>
    </div>

and my php script kontaktform.php has this code (so far) -

PHP:
<?php
$errors = [];

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get POST data
    $name = isset($_POST['name']) ? strip_tags(trim($_POST['name'])) : '';
    $email = isset($_POST['email']) ? trim($_POST['email']) : '';
    $phone = isset($_POST['phone']) ? trim($_POST['phone']) : '';
    $tilbudillustrationer = isset($_POST['tilbudillustrationer']) ? trim($_POST['tilbudillustrationer']) : '';
    $bestilling = isset($_POST['bestilling']) ? trim($_POST['bestilling']) : '';
    $ide = isset($_POST['ide']) ? trim($_POST['ide']) : '';
    $andet = isset($_POST['andet']) ? trim($_POST['andet']) : '';
    $about = isset($_POST['about']) ? trim($_POST['about']) : '';
    $email = isset($_POST['email']) ? strip_tags(trim($_POST['email'])) : '';
    
    
    
    // If no errors, send email
    if (empty($errors)) {
        // Recipient email address (replace with your own)
        $recipient = "[email protected]";

        // Additional headers
        $headers = "From: <$name> Mail: <$email> Telefon: <$phone> Besked: <$about>";

        // Send email
        if (mail($recipient, $message, $headers)) {
            echo "<span style=padding:30px;font-size:24px;font-family:georgia, serif;>Tak for e-mailen, jeg svarer så hurtigt jeg kan | Thank you for the email, I will reply as soon as I can </span><br><br><span style=padding:30px;font-size:20px;font-family:georgia, serif;><b>- Jakob i</b></span><br><br><br><span style=padding:30px;font-size:20px;font-family:georgia, serif;><a style=color:blue;cursor:pointer; onclick=history.back()>Tilbage >></a></span>";
        } else {
            echo "Failed to send email. Please try again later.";
        }
    } else {
        // Display errors
        echo "The form contains the following errors:<br>";
        foreach ($errors as $error) {
            echo "- $error<br>";
        }
    }
} else {
    // Not a POST request, display a 403 forbidden error
    header("HTTP/1.1 403 Forbidden");
    echo "You are not allowed to access this page.";
}
?>


The script sends information to my e-mail, but there are things left to sort out that I hope you can help me with -

1) the form doesn't send information from checkboxes
2) As I can read - I need some validation for the form, so it's secure


I really hope you can help me - I'm pretty stuck on this.

/ Jakob
 
Your PHP produces this warning:

Warning: Undefined variable $message in D:\web\kontaktform.php on line 27

Indeed, $message is not defined anywhere in this PHP file, and the contents or the 4 checkboxes is picked up but never used. So I am not surprised that it "does not send information from checkboxes". If it sends anything at all, I guess it would be an empty mail.

As for your second question, I would first get it to work, then worry about security 😉
 

Buy us a coffee!

Back
Top Bottom