0

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] => [email protected] 
    [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] => [email protected] 
    [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>\nReply-To: $Reply \nContent-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?

12
  • 2
    The [] array belongs in your inputs if you intend on using multiple values for the same input(s), not in processing it. Then you'd need a foreach and process the key values Commented Jan 31, 2016 at 16:59
  • 2
    @ShaileshKatarmal $_POST['mainmeal'] is an array, see this: ...[mainmeal] => Array ( [0] => Chicken [1] => Fish ) ... Commented Jan 31, 2016 at 17:04
  • 1
    You can do something like this: $mainmeal = implode(", ", $_POST['mainmeal']); Commented Jan 31, 2016 at 17:06
  • 2
    @ ServerSideSkittles - The question's unclear. But you can try what @RajdeepPaul wrote. We don't know what your inputs look like, or if you are using foreach somewhere that you didn't post. Edit: Great, glad to hear it worked. Commented Jan 31, 2016 at 17:09
  • 2
    All heil the awesome @Fred-ii- !!!!! Commented Jan 31, 2016 at 19:02

1 Answer 1

3

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:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.