dongshan1811 2013-01-22 07:05
浏览 28
已采纳

发送foreach循环PHP和JSON

Im not quiet sure where im going wrong but, when i use this mail function:

    <?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']) . "
";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "
";
$headers .= "BCC: email@address.co.za
";
$headers .= "MIME-Version: 1.0
";
$headers .= "Content-Type: text/html; charset=ISO-8859-1
";


$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);

it returns:

Main member data:
There are 3 Dependants
Dependant 0
Dependant 1
Dependant 2

instead of (as displayed in console):

Main member data:
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

There are 1 Dependants
Dependant 1
name : dep1
surname : dep1
id : 8202255133088
age : 30
gender : Male
townofbirth : dep1
cell : 0145264448
email : dep1
passport : 2222222222222
relationship : parent

here's the 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"
        }
    ]
}

I did get an answer on this: Parsing Duplicatable Form's JSON data to PHP for Mail

But just not the actual mailing..

Any help greatly appreciated

  • 写回答

1条回答 默认 最新

  • dongmi1221 2013-01-22 07:35
    关注

    The problem is on the printMember() function, you are doing echo and that doesn't return anything: Update your printMember function like this:

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化