PHP联系表格将空字段返回我的电子邮件

时间:2019-03-03 18:59:43

标签: php html

我在我的网站上创建了一个PHP联系人表格。表单正在发送电子邮件,但所有字段在电子邮件中均显示为空。我的代码在下面,是否有解决此问题的建议?

 <?php
    if ($_SERVER ["REQUEST_METHOD"] == "POST") {

        $car_marke = $_POST ['Marke'];
        $car_modell = $_POST ['Modell'];
        $car_vorname = $_POST ['Vorname'];
        $car_name = $_POST ['Name'];
        $car_adresse = $_POST ['Adresse'];
        $car_plz = $_POST ['PLZ/Ort'];
        $car_telefon = $_POST ['Telefon'];
        $car_email = $_POST ['Email'];
        $car_bemerkungen = $_POST ['Bemerkungen'];

        $from = 'test@gmail.com';
        $to = 'test@gmail.com';
        $subject = 'Formular';

        $body = "Marke: $car_marke\n Modell: $car_modell\n 
                        Vorname: $car_vorname\n Name: $car_name\n Adresse: $car_adresse\n 
                        PLZ/Ort: $car_plz\n    E-Mail: $car_email\n Telefon: $car_telefon\n Bemerkungen: $car_bemerkungen\n";
    }
 ?>

 <?php
        if ($_POST ['submit']) {
            if (mail ( $to, $subject, $body, $from )) {
                header ( "Location: http://www.exampledomain/send.php" );
            } else {
                echo '<p>Oops! An error occurred. Try sending your message again.</p>';
            }
        }
   ?>

1 个答案:

答案 0 :(得分:0)

对所有脚本使用相同的条件:

<?php

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

$car_marke = $_POST['Marke'];
$car_modell = $_POST['Modell'];
$car_vorname = $_POST['Vorname'];
$car_name = $_POST['Name'];
$car_adresse = $_POST['Adresse'];
$car_plz = $_POST['PLZ/Ort'];
$car_telefon = $_POST['Telefon'];
$car_email = $_POST['Email'];
$car_bemerkungen = $_POST['Bemerkungen'];

$from = 'test@gmail.com';  
$to = 'test@gmail.com'; 
$subject = 'Formular';

   $body = "Marke: $car_marke\n Modell: $car_modell\n 
Vorname: $car_vorname\n Name: $car_name\n Adresse: $car_adresse\n 
PLZ/Ort: $car_plz\n    E-Mail: $car_email\n Telefon: $car_telefon\n Bemerkungen: $car_bemerkungen\n";



   if (mail ($to, $subject, $body, $from)) { 
    mail ($from, $subject, $body, $to) 
    header("Location: http://www.exampledomain/send.php");
    } else { 
      echo '<p>Oops! An error occurred. Try sending your message again.</p>'; 
      }
     }
     ?>