Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
发送foreach循环PHP和JSON_Php_Jquery_Json - Fatal编程技术网

发送foreach循环PHP和JSON

发送foreach循环PHP和JSON,php,jquery,json,Php,Jquery,Json,我不确定我哪里出了问题,但当我使用此邮件功能时: <?php function printMember($member) { foreach($member as $key=>$value) { echo "$key : $value <br />"; } } $to = 'email@address.co.za'; $subject = 'Application Form'; $headers = "From: " . st

我不确定我哪里出了问题,但当我使用此邮件功能时:

    <?php
function printMember($member) {
    foreach($member as $key=>$value) {
        echo "$key : $value <br />";
    }
}

$to = 'email@address.co.za';

$subject = 'Application Form';

$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "BCC: email@address.co.za\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


$json = $_POST['parameters'];
$json_string = stripslashes($json);
$data = json_decode($json_string, true);

$depCount = count($data["dependants"]);


$msg .= "<h2>Main member data:</h2>";
$msg .= printMember($data["mainmember"]);

$msg .= "<h2>There are $depCount Dependants</h2>";

foreach ($data["dependants"] as $index => $dependant) {
   $msg .= "<h2>Dependant $index</h2>";
   $msg .= printMember($dependant);
}

 mail($to, $subject, $msg, $headers);
而不是(如控制台中所示):

以下是JSON:

{
    "mainmember": {
        "name": "name",
        "surname": "surename",
        "id": "6110190027087",
        "age": "51",
        "gender": "Female",
        "townofbirth": "george",
        "email": "naem@gmail.com",
        "contact": "0512148615",
        "passport": "1111111111111",
        "postal": "test",
        "postal_code": "4545",
        "residential": "test",
        "residential_code": "4545"
    },
    "dependants": [
        {
            "name": "dep1",
            "surname": "dep1",
            "id": "8202255133088",
            "age": "30",
            "gender": "Male",
            "townofbirth": "dep1",
            "cell": "0145264448",
            "email": "dep1",
            "passport": "2222222222222",
            "relationship": "parent"
        }
    ]
}
我确实得到了答案:

但不是真正的邮寄


非常感谢您的任何帮助

问题出在
printMember()
函数上,您正在执行
echo
,但没有返回任何内容:按如下方式更新printMember函数:

function printMember($member) {
    foreach($member as $key=>$value) {
        //Fill the aux string first
        $str.= "$key : $value <br />";
    }
    //string that will be added to $msg variable inside the loop
    return $str;
}
函数printMember($member){
foreach($key=>$value的成员){
//首先填充辅助字符串
$str.=“$key:$value
”; } //将添加到循环内$msg变量的字符串 返回$str; }
您正在执行“echo”,而不是返回。检查我的答案
{
    "mainmember": {
        "name": "name",
        "surname": "surename",
        "id": "6110190027087",
        "age": "51",
        "gender": "Female",
        "townofbirth": "george",
        "email": "naem@gmail.com",
        "contact": "0512148615",
        "passport": "1111111111111",
        "postal": "test",
        "postal_code": "4545",
        "residential": "test",
        "residential_code": "4545"
    },
    "dependants": [
        {
            "name": "dep1",
            "surname": "dep1",
            "id": "8202255133088",
            "age": "30",
            "gender": "Male",
            "townofbirth": "dep1",
            "cell": "0145264448",
            "email": "dep1",
            "passport": "2222222222222",
            "relationship": "parent"
        }
    ]
}
function printMember($member) {
    foreach($member as $key=>$value) {
        //Fill the aux string first
        $str.= "$key : $value <br />";
    }
    //string that will be added to $msg variable inside the loop
    return $str;
}