获取表单阵列并发送到电子邮件

时间:2017-08-08 15:54:57

标签: php arrays forms

所以我还在学习PHP,而且我有一个有4个数组的表单。我试图将数组发送到我的电子邮件以及其他一些信息。我一直收到的电子邮件说" array,array" (然后其余信息正确显示)。我无法弄清楚我错过了什么。

我可以正确显示父信息,但我仍然坚持要将子信息转换为电子邮件。

if (isset($_POST['submit'])){
    // Parent Info
    $pFname = mysql_escape_string($_POST['pFname']);
    $pLname = mysql_escape_string($_POST['pLname']);
    $email = mysql_escape_string($_POST['pEmail']);
    $street = mysql_escape_string($_POST['street']);
    $city = mysql_escape_string($_POST['city']);
    $state = mysql_escape_string($_POST['state']);
    $zip = mysql_escape_string($_POST['zip']);

    // Child Info
    $name = mysql_escape_string($_POST['Name']);
    $birthday = mysql_escape_string($_POST['Birthday']);
    $gender = mysql_escape_string($_POST['Gender']);
    $rel = mysql_escape_string($_POST['Rel']);

    $msg1 = "From: " . ' ' . $pFname . ' ' . $pLname  . ' ' . ' ' . $pEmail 
    . ' ' . ' ';

    foreach ($_POST['Name'] as $names){
        $name = $names;
    }

    foreach ($_POST['Birthday'] as $birthdays){
        $birthday = $birthdays;
    }

    foreach ($_POST['Gender'] as $genders){
        $gender = $genders;
    }

    foreach ($_POST['Rel'] as $rels) {
        $rel = $rels;
    }

    $msg = "Name: " . " " . $name . " - " . $gender .  "Birthday: " . 
     $birthday . " " . "Relationship: " . $rel . "\n\r";

    $to = 'joe@joe.com;
    $subject = 'Birthday Club Application';
    $msg = $msg . "\r\n";
    $msg .= "First Name: " . $pFname . "\r\n";
    $msg .= "Last Name: " . $pLname . "\r\n";
    $msg .= "Email: " . $email . "\r\n";
    $msg .= "Street: " . $street . "\r\n";
    $msg .= "City: " . $city . "\r\n";
    $msg .= "State: " . $state . "\r\n";
    $msg .= "Zip: " . $zip . "\r\n";

    $headers = 'From: ' . $pEmail . "\r\n";
    $headers1 = 'From: mycompany.com';
    /*$goto_after_mail = "birthdayClub.php";*/

    mail($to, $subject, $msg, $msg1, $headers);

    header("Location: " . $goto_after_mail);
}

以下是HTML:

<form id="parent-info" method="post" action="<?php echo 
 htmlspecialchars($_SERVER[" PHP_SELF "])?>">

    <h1>Guardian Information</h1>
    <h4>*All fields required*</h4>

    <input type="text" name="pFname" class="parent-entry" placeholder="First Name:" >

    <input type="text" name="pLname" class="parent-entry" placeholder="Last Name:" >

    <input type="text" name="pEmail" class="parent-entry" placeholder="Email:" >

    <input type="text" name="street" class="parent-entry" placeholder="Street Address:" >

    <input type="text" name="city" class="parent-entry" placeholder="City:" >

    <input type="text" name="state" class="parent-entry" placeholder="State:" >

    <input type="text" name="zip" class="parent-entry" placeholder="Zip Code:" >
    <br>

    <h2>Child Information</h2>
    <h4>*All fields required*</h4>

    <div class="child-form">

        <input type="text" name="Name[]" placeholder="Childs Name:" class="parent-entry" value " "; >

        <input type="text" name="Gender[]" placeholder="Gender:" class="parent-entry" value " ";>

        <input type="text" name="Birthday[]" placeholder="Ex: 1/1/01:" class="parent-entry" value " "; >

        <input type="text" name="Rel[]" placeholder="Relation to child:" class="parent-entry" value " ";>

    </div>

1 个答案:

答案 0 :(得分:0)

 foreach ($_POST['Name'] as $names)

这一行表明$_POST['Name']是一个数组。如果是这样的话,你会遇到这条线的问题:

$name = mysql_escape_string($_POST['Name']);

该函数需要一个字符串。当您为数组提供数组时,它会将其转换为字符串&#34; 数组&#34;。这同样适用于所有子数组。您在阵列上使用mysql_escape_string

你要做的事情,因为你似乎有一系列孩子,就是循环遍历那个数组并拿起每个孩子的信息添加到电子邮件中。

另一种选择,如果您不想遍历所有这些数组,则使用print_r函数,并将其第二个可选参数设置为true。这会将数组转换为用户可读的字符串。

更改

$name = mysql_escape_string($_POST['Name']);

要:

$name = print_r($_POST['Name'], true);

Live demo