使用动态创建的表单值将变量设置为php中的POST请求

时间:2015-09-21 15:58:08

标签: php forms isset

我有一个表单,可以使用javascript动态添加行,因此也可以通过下一个数字(即name1name2name3等)递增名称标记。

如何检查给定值是否存在,然后将其分配给变量,以便将包含的表单信息发送到电子邮件地址?

到目前为止,我正在使用这个:

<?php
// My modifications to mailer script from:
// http://blog.teamtreehouse.com/create-ajax-contact-form
// Added input sanitizing to prevent injection

// Only process POST requests.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form fields and remove whitespace.
    $Firma = strip_tags(trim($_POST["Firma"]));
    $Firma = str_replace(array("\r","\n"),array(" "," "),$Firma);
    $Vorname = strip_tags(trim($_POST["Vorname"]));
    $Vorname = str_replace(array("\r","\n"),array(" "," "),$Vorname);
    $Name = strip_tags(trim($_POST["Name"]));
    $Name = str_replace(array("\r","\n"),array(" "," "),$Name);
    $Strasse = strip_tags(trim($_POST["Strasse"]));
    $Strasse = str_replace(array("\r","\n"),array(" "," "),$Strasse);
    $PLZ = strip_tags(trim($_POST["PLZ"]));
    $PLZ = str_replace(array("\r","\n"),array(" "," "),$PLZ);
    $Ort = strip_tags(trim($_POST["Ort"]));
    $Ort = str_replace(array("\r","\n"),array(" "," "),$Ort);
    $Telefon = strip_tags(trim($_POST["Telefon"]));
    $Telefon = str_replace(array("\r","\n"),array(" "," "),$Telefon);
    $Email = filter_var(trim($_POST["E-Mail"]), FILTER_SANITIZE_EMAIL);
    $Abholadresse = strip_tags(trim($_POST["Abholadresse"]));
    $Abholadresse = str_replace(array("\r","\n"),array(" "," "),$Abholadresse);
    $PLZ_Abholadresse = strip_tags(trim($_POST["PLZ_Abholadresse"]));
    $PLZ_Abholadresse = str_replace(array("\r","\n"),array(" "," "),$PLZ_Abholadresse);
    $Ort_Abholadresse = strip_tags(trim($_POST["Ort_Abholadresse"]));
    $Ort_Abholadresse = str_replace(array("\r","\n"),array(" "," "),$Ort_Abholadresse);
    $Bemerkungen = trim($_POST["Bemerkungen"]);

    $Materialien = trim($_POST["materialien0"]);
    $Anzahl = trim($_POST["anzahl0"]);
    $Gebinde = trim($_POST["einheit0"]);
    $Austausch = trim($_POST["austausch0"]);
    $Additional = trim($_POST["additional0"]);

    $Materialien_1 = trim($_POST["materialien1"]);
    $Anzahl_1 = trim($_POST["anzahl1"]);
    $Gebinde_1 = trim($_POST["einheit1"]);
    $Austausch_1 = trim($_POST["austausch1"]);
    $Additional_1 = trim($_POST["additional1"]);

    // Check that data was sent to the mailer.
    if ( empty($Name) OR empty($Strasse) OR empty($PLZ) OR empty($Ort)
        OR empty($Telefon) OR !filter_var($Email, FILTER_VALIDATE_EMAIL)) {
        // Set a 400 (bad request) response code and exit.
        http_response_code(400);
        echo "Ups! Leider ist ein Fehler aufgetreten. Bitte stellen Sie sicher, dass Sie alle Felder korrekt eingesetzt haben.";
        exit;
    }

    // Set the recipient email address.
    // FIXME: Update this to your desired email address.
    $recipient = "asymmetrisch@gmail.com";

    // Set the email subject.
    $subject = "Kontaktanfrage von unserer Webseite: $Name";

    // Build the email content.
    $email_content = "Firma: $Firma\n";
    $email_content .= "Vorname: $Vorname\n";
    $email_content .= "Name: $Name\n";
    $email_content .= "Strasse: $Strasse\n";
    $email_content .= "PLZ: $PLZ\n";
    $email_content .= "Ort: $Ort\n";
    $email_content .= "Telefon: $Telefon\n";
    $email_content .= "Email: $Email\n\n";
    $email_content .= "Abholadresse: $Abholadresse\n";
    $email_content .= "PLZ_Abholadresse: $PLZ_Abholadresse\n";
    $email_content .= "Ort_Abholadresse: $Ort_Abholadresse\n";
    $email_content .= "Bemerkungen:\n$Bemerkungen\n\n";

    $email_content .= "Material: $Materialien\n";
    $email_content .= "Anzahl: $Anzahl\n";
    $email_content .= "Einheit: $Gebinde\n";
    $email_content .= "Austausch: $Austausch\n";
    $email_content .= "Zusaetzliche Gebinde: $Additional\n\n";

    $email_content .= "Material: $Materialien_1\n";
    $email_content .= "Anzahl: $Anzahl_1\n";
    $email_content .= "Einheit: $Gebinde_1\n";
    $email_content .= "Austausch: $Austausch_1\n";
    $email_content .= "Zusaetzliche Gebinde: $Additional_1\n";



    // Build the email headers.
    $email_headers = "From: $Vorname $Name $Firma <$Email>";

    // Send the email.
    if (mail($recipient, $subject, $email_content, $email_headers)) {
        // Set a 200 (okay) response code.
        http_response_code(200);
        echo "Danke Ihnen, wir haben Ihre Nachricht erhalten und werden uns bei Ihnen melden.";
    } else {
        // Set a 500 (internal server error) response code.
        http_response_code(500);
        echo "Ups! Etwas ist falsch gelaufen; leider konnte Ihre Nachricht nicht versendet werden.";
    }

} else {
    // Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
    echo "Es ist ein Problem aufgetreten. Ihre Nachricht konnte leider nicht versendet werden.";
}
?>

值得一提的是,只有部分字段是动态的,从$Materialien开始。正如你所看到的,我也声明了一个变量$Materialien_1并且可以继续这样(即$Materialien_2,3等),只需硬编码一堆变量以确保我能够获得所有变量无论如何,电子邮件中的信息。但当然那将毫无意义。我很想得到一个解决方案并从中学到一些东西。

4 个答案:

答案 0 :(得分:0)

您可以在每次迭代时使用循环并检查post属性的存在:

for($n = 0; isset($_POST["materialien$n"]); $n++) {
    $Materialien = trim($_POST["materialien$n"]);
    $Anzahl = trim($_POST["anzahl$n"]);
    $Gebinde = trim($_POST["einheit$n"]);
    $Austausch = trim($_POST["austausch$n"]);
    $Additional = trim($_POST["additional$n"]);

    $email_content .= "\nMaterial: $Materialien\n";
    $email_content .= "Anzahl: $Anzahl\n";
    $email_content .= "Einheit: $Gebinde\n";
    $email_content .= "Austausch: $Austausch\n";
    $email_content .= "Zusaetzliche Gebinde: $Additional\n";
}

答案 1 :(得分:0)

您可以使用foreach循环来获取动态添加的字段值
你可以试试::

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    foreach($_POST as $key=>$value)
    {
         $email_content .= "$key: $value\n";
    }
}

答案 2 :(得分:0)

由于您的关注点似乎是检查共享相同标签的随机数量的属性,因此您只需通过$_POST传递数组即可获得动态字段。

这是通过使用这样的表单输入来完成的:

<input type="text" name="label[]" />
<input type="text" name="label[]" />
<input type="text" name="label[]" />

将这些输入提交给PHP会将它们转换为数组。在这个例子中:

$_POST['label'][0]
$_POST['label'][1]
$_POST['label'][2]

这使您可以轻松地迭代它们:

foreach($_POST['label'] as $value) {
    // validate your value here using $value
}

您可以强制执行以下索引:

<input type="text" name="label[3]" />
<input type="text" name="label[4]" />
<input type="text" name="label[5]" />

或者您可以通过更改结构直接对相关字段进行分组:

<input type="text" name="group[3][name]" />
<input type="text" name="group[3][materialien]" />
<input type="text" name="group[3][anzahl]" />

在迭代group时,最后一个结构将返回一个数组:

foreach($_POST['group'] as $arr) {
    // you can use the following indexes:
    // $arr['name']
    // $arr['materialien']
    // $arr['anzahl']
}

答案 3 :(得分:0)

没问题,我只是发布代码结构 您可以使用并尝试将变量名称保留为小写字母,以便进行良好的编码练习。 也不需要使用

$Firma = str_replace(array("\r","\n"),array(" "," "),$Firma);

PHP修剪功能将修剪&#34; \ r&#34;和&#34; \ n&#34;
祝你未来好运。
所以你可以像这样使用foreach循环:

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form fields and remove whitespace.
    $Firma = strip_tags(trim($_POST["Firma"]));
    $Firma = str_replace(array("\r","\n"),array(" "," "),$Firma);
    $Vorname = strip_tags(trim($_POST["Vorname"]));
    $Vorname = str_replace(array("\r","\n"),array(" "," "),$Vorname);
    $Name = strip_tags(trim($_POST["Name"]));
    $Name = str_replace(array("\r","\n"),array(" "," "),$Name);
    $Strasse = strip_tags(trim($_POST["Strasse"]));
    $Strasse = str_replace(array("\r","\n"),array(" "," "),$Strasse);
    $PLZ = strip_tags(trim($_POST["PLZ"]));
    $PLZ = str_replace(array("\r","\n"),array(" "," "),$PLZ);
    $Ort = strip_tags(trim($_POST["Ort"]));
    $Ort = str_replace(array("\r","\n"),array(" "," "),$Ort);
    $Telefon = strip_tags(trim($_POST["Telefon"]));
    $Telefon = str_replace(array("\r","\n"),array(" "," "),$Telefon);
    $Email = filter_var(trim($_POST["E-Mail"]), FILTER_SANITIZE_EMAIL);


    // Check that data was sent to the mailer.
    if ( empty($Name) OR empty($Strasse) OR empty($PLZ) OR empty($Ort)
        OR empty($Telefon) OR !filter_var($Email, FILTER_VALIDATE_EMAIL)) {
        // Set a 400 (bad request) response code and exit.
        http_response_code(400);
        echo "Ups! Leider ist ein Fehler aufgetreten. Bitte stellen Sie sicher,           dass Sie alle Felder korrekt eingesetzt haben.";
        exit;
      }

    // Set the recipient email address.
    // FIXME: Update this to your desired email address.
    $recipient = "asymmetrisch@gmail.com";

    // Set the email subject.
    $subject = "Kontaktanfrage von unserer Webseite: $Name";
    $email_content='';
    if(!empty($_POST))
    {
        foreach($_POST as $field_name=>$field_value)
        { 
             $field_value = strip_tags(trim($field_value)); 
             $email_content .= "$field_name: $field_value\n";
        } 
    }



    // Build the email headers.
    $email_headers = "From: $Vorname $Name $Firma <$Email>";

    // Send the email.
    if (mail($recipient, $subject, $email_content, $email_headers)) {
        // Set a 200 (okay) response code.
        http_response_code(200);
        echo "Danke Ihnen, wir haben Ihre Nachricht erhalten und werden uns bei Ihnen melden.";
    }
    else 
   {
        // Set a 500 (internal server error) response code.
        http_response_code(500);
        echo "Ups! Etwas ist falsch gelaufen; leider konnte Ihre Nachricht nicht versendet werden.";
    }

} 
else
{
    // Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
    echo "Es ist ein Problem aufgetreten. Ihre Nachricht konnte leider nicht    versendet werden.";
}
?>