-1

i've got some Problems with my Contact-Form. I tried to get the form i want to use from some Tutorials, but its not working. But i dont know why :D

I hope you guys can help me, maybe its just a little error :) Should it work, or do i have to edit some settings on my Webspace?

    <?php
    if (isset($_POST["submit"])) {
        $firma = $_POST['inputFirma'];
        $vorname = $_POST['inputVorname'];
        $telefon = $_POST['inputTelefon'];
        $plzort = $_POST['inputPlzOrt'];
        $adresse = $_POST['inputStraßeHausnummer'];
        $firma = $_POST['input'];
        $name = $_POST['inputName'];
        $email = $_POST['inputEmail'];
        $message = $_POST['inputNachricht'];
        $human = intval($_POST['inputHuman']);
        $from = 'Demo Contact Form'; 
        $to = 'hasse.j@gmx.de'; 
        $subject = 'Nachricht des Kontaktformulars';

        $body = "From: $firma\n $vorname\n $name\n PLZ/Ort: $plzort\n Adresse: $adresse\n  E-Mail: $email\n Message:\n $message";

        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Bitte geben Sie Ihren Namen ein';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Bitte geben Sie eine echte E-Mail-Adresse ein';
        }

        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Bitte geben Sie eine Nachricht ein';
        }
        //Check if simple anti-bot test is correct
        if ($human !== 5) {
            $errHuman = 'Bitte rechnen Sie erneut';
        }

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    }
}
    }
?>

and the HTML:

<form action="" method="post">
    <div class="form-group">
        <label for="inputFirma">Firma</label>
        <input type="text" class="form-control" id="inputFirma">
    </div>
    <div class="form-group">
        <label for="inputName">*Name</label>
        <input type="text" class="form-control" id="inputName">
    </div>
    <div class="form-group">
        <label for="inputVorname">Vorname</label>
        <input type="text" class="form-control" id="inputVorname">
    </div>
    <div class="form-group">
        <label for="inputStraßeHausnummer">Straße und Hausnummer</label>
        <input type="text" class="form-control" id="inputStraßeHausnummer">
    </div>

    <div class="form-group">
        <label for="inputPlzOrt">Ort / Postleitzahl</label>
        <input type="text" class="form-control" id="inputPlzOrt">            
    </div>
    <div class="form-group">
        <label for="inputTelefon">Telefon</label>
        <input type="text" class="form-control" id="inputTelefon">            
    </div>
    <div class="form-group">
        <label for="inputEmail">*E-Mail</label>
        <input type="email" class="form-control" id="inputEmail">
    </div>   
    <div class="form-group">
        <label for="inputNachricht">*Ihre Nachricht</label>            
        <textarea type="text" class="form-control" cols="40" rows="5" id="inputNachricht"></textarea>        
    </div>   
    <br>
    <div class="form-group">
        <label for="inputHuman" class="col-sm-2 control-label">2 + 3 = ?</label>        
        <input type="text" class="form-control" id="inputHuman">            
    </div>    
    <br>
    <button type="submit" class="btn btn-primary margin-bot-50">Senden</button>
</form>
Kjarudi
  • 15
  • 5

1 Answers1

0
<?php
    if (isset($_POST["submit"])) {
        $firma = $_POST['inputFirma'];
        $vorname = $_POST['inputVorname'];
        $telefon = $_POST['inputTelefon'];
        $plzort = $_POST['inputPlzOrt'];
        $adresse = $_POST['inputStraßeHausnummer'];
        $firma = $_POST['input'];
        $name = $_POST['inputName'];
        $email = $_POST['inputEmail'];
        $message = $_POST['inputNachricht'];
        $human = intval($_POST['inputHuman']);
        $from = 'Demo Contact Form'; 
        $to = 'hasse.j@gmx.de'; 
        $subject = 'Nachricht des Kontaktformulars';

        $body = "From: $firma\n $vorname\n $name\n PLZ/Ort: $plzort\n Adresse: $adresse\n  E-Mail: $email\n Message:\n $message";

        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Bitte geben Sie Ihren Namen ein';
        } else { $errName = false; }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Bitte geben Sie eine echte E-Mail-Adresse ein';
        } else { $errEmail = false; }

        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Bitte geben Sie eine Nachricht ein';
        } else { $errMessage = false; }
        //Check if simple anti-bot test is correct
        if ($human !== 5) {
            $errHuman = 'Bitte rechnen Sie erneut';
        } else { $errHuman = false; }


// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    }
}
    }
?>

In your if statment where you checking error I added else so you define values if there is no error. Hope you understand what i did and this can fix your problem. Because in your if

if (!$errName && !$errEmail && !$errMessage && !$errHuman) {}

you checking things that are not defined at all before if there is no error.

Also your html code <form action="" method="post"> dont have filled action field so you need to fill it with php file where you submitting variables

Standej
  • 749
  • 1
  • 4
  • 11
  • My problem is, that i can send the form - but i dont receive an E-Mail. Here is the whole website: http://opti-serv-os.de/beta/kontakt.php – Kjarudi Nov 28 '15 at 17:02
  • you can't send an email because this if statment is never true `if (!$errName && !$errEmail && !$errMessage && !$errHuman)` Hope you understand me. If your form provide you all fields correct variables $err... not exists so you need to assign to them value false like i did in example so then this if statment will be true if all fields are filled and send an email @Kjarudi – Standej Nov 28 '15 at 17:09
  • i used your code. I just copied this stuff from a tutorial side, cause my php is very bad :P This error is not necessary, ill try to delete it. But it should work like this normally? – Kjarudi Nov 28 '15 at 17:28
  • If you fix what i told you it should work i think (At least there is a possibility to enter in if statment if all conditions are met) – Standej Nov 28 '15 at 18:07
  • i edited the whole code - now without IF. Now its working, thats fine for the beginning :) Thank you – Kjarudi Nov 28 '15 at 18:16