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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据