0

My old Contact-Form shows this Error when it was sended:

Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /kunden/273982_79100/webseiten/kontakt/index.php on line 126

This is the PHP-Code for the Contact-Form:

<?php
$Empfaenger = "mail@mailinfo.com";

$headers .= "Content-Type: text/plain; charset = \"UTF-8\";\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= "\n";

if($_POST['Send']) {
    if(empty($_POST['vornameundnachnachme']) || empty($_POST['adresse']) || empty($_POST['plzort']) || empty($_POST['email']) || empty($_POST['betreff']) || empty($_POST['datenschutz'])) {
        echo"<p style='margin-top: 40px;'><div id='alert'>Ihre Online-Anfrage wurde nicht versendet.<ul style='margin-bottom: 0px;'><li>rot markierte Felder müssen ausgefüllt werden</li></ul></div></p>";
    } else {
        $Mailnachricht = "Question \n\n";
        while(list($Formularfeld, $Wert)=each($_POST) {
            if($Formularfeld!="Send") {
                $Mailnachricht .= $Formularfeld.": ".$Wert."\n";
            }
        }
        $Mailnachricht .= "\nDatum/Zeit: ";
        $Mailnachricht .= date("d.m.Y H:i:s");
        $Mailbetreff = "Anfrage";
        $Mailbetreff .= $_POST['Emailadresse'];
        mail($Empfaenger, $Mailbetreff, $Mailnachricht, "From: ".$_POST['Emailadresse']);
        echo"<p style='margin-top: 40px;'><div id='sent'><h2><i class='fal fa-thumbs-up'></i> Gesendet!</h2></div></p>";
    }
}
?>

I have read some workarounds about that, but nothing worked for me. I think i make a mistake in one of the lines... don't know where...

Maybe anybody can help me!

Thanks a lot! Alex

Giacomo M
  • 4,450
  • 7
  • 28
  • 57
Alex
  • 3
  • 1
  • 4
  • 1
    Can you share what workarounds you have tried? – tomerpacific May 26 '20 at 10:04
  • 1
    This is not an error, it is a warning that in future revisions of PHP your code may not work, currently it should work. The line `while(list($Formularfeld, $Wert)=each($_POST) {` is the one throwing the warning because you use the *each()* function there. Have a look at the following [Workarounds](https://stackoverflow.com/questions/46492621/how-to-resolve-this-deprecated-function-each-php) Explain which workarounds you have tried and why they are not working. – Pau Coma Ramirez May 26 '20 at 11:04

2 Answers2

1

The list($Formularfeld, $Wert)=each($_POST) is an outdated structure and should not be used anymore - I can only assume you found a really old tutorial somewhere.

The correct approach would be to preprocess the $_POST global and create a proper array with it, then iterate through it using foreach

foreach($_POST as $formularfeld => $wert) {
    $mailinfo[$formularfeld] = $value; //Do NOT forget validation here, huge security problem
}

This way, we have a nice array called $mailinfo we can use later on, like this:

foreach($mailinfo as $formularfeld => $wert) {
    if($formularfeld != "Send") {
        $Mailnachricht .= $formularfeld.": ".$wert."\n";
    }
}
Realitätsverlust
  • 3,941
  • 2
  • 22
  • 46
0

The function is no longer supported. You can disable the warning with an "@" in front of the function.

Better would be to use an alternative function like foreach

MThiele
  • 387
  • 1
  • 9