PHP表单 - 回显值数组

时间:2016-01-31 16:57:51

标签: php arrays forms

我有一个从中获取的基本字段,但我想放入一些数组值。以下是提交表单时的发布数据示例。

successArray ( 
    [rsvpname] => wewqewqe 
    [rsvpemail] => ewwqe@ffdfs.com 
    [rsvpphone] => 23232 
    [rsvpcomments] => 21321 
    [guests] => 1 
    [mainmeal] => Array ( 
        [0] => Chicken 
        [1] => Fish 
    ) 
    [secondmeal] => Array ( 
        [0] => Chicken 
        [1] => Fish 
    ) 
    [desert] => Array ( 
        [0] => Chocolate Cake 
        [1] => Pumpkin Cheesecake 
    ) 
    [menunotes] => Array ( 
        [0] => 231312321 
        [1] => 231312321 
    ) 
    [to] => myemail@gmail.com 
    [subject] => RSVP form message 
    [submit] => SEND RSVP 
)

要添加数组,我已将[]附加到所述字段的名称。这是我的

processing.php。

$mainmeal = stripslashes($_POST['mainmeal[]']);
    $secondmeal = stripslashes($_POST['secondmeal[]']);
    $desert = stripslashes($_POST['desert[]']);
    $menunotes = stripslashes($_POST['menunotes[]']);

然后它放入电子邮件但我不知道如何回应阵列。

// Let's send the email.
    if(!$error) {
        $messages="From: $email <br>";
        $messages.="Name: $name <br>";
        $messages.="Email: $email <br>";    
        $messages.="Phone: $phone <br>";
        $messages.="Message: $message <br><br>";
        $messages.="No. of guests: $guests <br>";
        $messages.="Meal selection: $mainmeal <br>";
        $messages.="Second Meal selection: $secondmeal <br>";
        $messages.="Desert selection: $desert <br>";
        $messages.="Menu notes: $menunotes <br>";
        $emailto=$to;

        $mail = mail($emailto,$subject,$messages,"from: $from <$Reply>\nReply-To: $Reply \nContent-type: text/html");   

        if($mail) {
            echo 'success';
            print_r($_POST);
        }
    } else {
        echo '<div class="error">'.$error.'</div>';
    }

所有字段名称,电子邮件电话等都已发送但不是数组。我如何制作它以便它也会发送数组?

1 个答案:

答案 0 :(得分:3)

由于$_POST['mainmeal']$_POST['secondmeal]等是数组,因此使用implode()函数来连接数组元素,如下所示:

$mainmeal = implode(", ", $_POST['mainmeal']);
$secondmeal = implode(", ", $_POST['secondmeal']);
// so on

以下是参考资料: