douwaz34842 2016-01-31 16:57
浏览 60
已采纳

PHP表单 - 回显值数组

I have a from which fetched basic fields but I would like to put in some values which are arrays. Here is an example of the post data when the form is submitted.

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 
)

To add an array I have appended [] to the names of said fields. Here is my

processing.php.

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

Then it puts into an email but I don't know how to echo out the array.

// 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>
Reply-To: $Reply 
Content-type: text/html");   

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

All the fields name, email phone etc get sent but not the arrays. How do I make it so it will send the arrays also?

  • 写回答

1条回答 默认 最新

  • duanlu1279 2016-01-31 17:20
    关注

    Since $_POST['mainmeal'], $_POST['secondmeal] etc. are arrays, use implode() function to join the array elements, like this:

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

    Here's the reference:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 链接问题 C++LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接