2016-09-15 309 views
-1

出於某種原因,我得到未定義的變量和字符串數組轉換 我不明白,爲什麼無論這些正在發生的事情未定義的變量變量聲明

Notice: Undefined variable: body in C:\Users\New\Desktop\xampp\htdocs\yakov\sendemail.php</b> on line <b>20 

Notice: Array to string conversion in C:\Users\New\Desktop\xampp\htdocs\yakov\sendemail.php on line 33 
"Array'services.html: ''services.html'\n\n'new york: ''new york'\n\n'new york: ''new york'\n\n'round_trip: ''round_trip'\n\n'2016-09-16: ''2016-09-16'\n\n'2016-09-23: ''2016-09-23'\n\n'nonstop: ''nonstop'\n\n'flexible: ''flexible'\n\n'Business: ''Business'\n\n'1 Adult: ''1 Adult'\n\n'some: ''some'\n\n'one: ''one'\n\n'[email protected]: ''[email protected]'\n\n'new york: ''new york'\n\n'dsfa\n: ''dsfa\n'\n\n'4127117117: ''4127117117'\n\n'me: ''me'\n\n;" 

這裏是我的代碼這是導致我試圖玩弄它的問題

<?php 
    header('Content-type: application/json'); 
    $status = array(
     'type'=>'success', 
     'message'=>'Thank you for contacting us. We will contact you as early as possible.' 
    ); 
    //print phpinfo(); 
    error_reporting(-1); 
ini_set('display_errors', 'On'); 
//set_error_handler("var_dump"); 
$body; 
$email; 
$subject; 
$email_from; 
$email_to = '[email protected]'; 
if (!empty($_REQUEST)) { 
    $body; 
    foreach($_REQUEST as $key => $val) { 
     if (isset($_REQUEST[$key])) { 
      $body .= "'". $_REQUEST[$key] .": '" . $val . "\n\n"; 
     } 

    } 
$email = isset($_REQUEST['email']) ? trim(stripslashes($_REQUEST['email'])) : "NA"; 
$subject = isset($_REQUEST['subject']) ? trim(stripslashes($_REQUEST['subject'])) : "NA"; 
    $body .= ";"; 
    $email_from = $email; 
    //$email_to = '[email protected]';// your email 
    $body; 
} 
$success = mail($email_to, $subject, $body, 'From: <'.$email_from.'>'); 

    echo json_encode($status .$body); 
//} 
    die; 

任何其他建議,將不勝感激,因爲我是新來的後端

+1

首先:你正在做的只是$ body;很多。首先將它定義爲一個字符串,不要重新定義它($ body ='';) – user3791775

回答

1

我不知道你這個做什麼:

$body; 

我在你的代碼看到三個流浪$body;的。讓那些人離開那裏。正如在評論中提到的,只是把它定義爲一次在頂部的字符串:

$body = ""; 

然後你就可以連接其他字符串給它的所有你的願望。

數組字符串錯誤可能是由於這樣的事實,你試圖連接一個字符串數組:

echo json_encode($status .$body); 
// ^-- this won't work. $status is an array. $body is a string. 

如果你只是呼應的是JSON的樂趣,你總是可以首先將你的身體字符串添加到狀態數組中,然後將其回顯出來:

$status['body'] = $body; 
echo json_encode($status);