HTML / PHP联系表单问题

时间:2015-11-28 16:38:11

标签: php html forms

我的联系表格有问题。 我试图从一些教程中获取我想要使用的表单,但它不起作用。 但我不知道为什么:D

我希望你们能帮助我,也许只是一点点错误:) 它应该工作,还是我必须在我的网站空间上编辑一些设置?

    <?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>';
    }
}
    }
?>

和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>

1 个答案:

答案 0 :(得分: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>';
    }
}
    }
?>

在您检查错误的if语句中,我添加了else,以便在没有错误时定义值。希望你明白我做了什么,这可以解决你的问题。 因为在你的if

if (!$errName && !$errEmail && !$errMessage && !$errHuman) {}
如果没有错误,你可以检查之前没有定义的东西。

也是你的HTML代码     <form action="" method="post"> 没有填充操作字段,所以你需要填写提交变量的php文件