php
  • jquery
  • json
  • 2013-01-22 57 views 0 likes 
    0

    我不是在那裏我去錯了安靜肯定,但,當我使用這個郵件功能:發送foreach循環PHP和JSON

    <?php 
    function printMember($member) { 
        foreach($member as $key=>$value) { 
         echo "$key : $value <br />"; 
        } 
    } 
    
    $to = '[email protected]'; 
    
    $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 protected]\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); 
    

    返回:

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

    代替(如顯示在控制檯):

    Main member data: 
    name : name 
    surname : surename 
    id : 6110190027087 
    age : 51 
    gender : Female 
    townofbirth : george 
    email : [email protected] 
    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 
    

    這裏的JSON:

    { 
        "mainmember": { 
         "name": "name", 
         "surname": "surename", 
         "id": "6110190027087", 
         "age": "51", 
         "gender": "Female", 
         "townofbirth": "george", 
         "email": "[email protected]", 
         "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" 
         } 
        ] 
    } 
    

    我沒有得到這個答案:Parsing Duplicatable Form's JSON data to PHP for Mail

    但只是沒有實際的郵件..

    任何幫助,不勝感激

    +0

    您是doint'echo',而不是返回。檢查我的答案 –

    回答

    2

    的問題是在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; 
    } 
    
    +0

    對不起,我的錯我現在完美:) –

    +0

    好吧,它爲我工作;) –

    相關問題