PHP中未定义的变量变量声明和数组到字符串的转换

时间:2016-09-15 16:53:44

标签: php ajax xmlhttprequest

由于某种原因,我得到未定义的变量和字符串到数组转换 我不明白为什么会发生这些事情

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'someonesemail@gmail.com:  ''someonesemail@gmail.com'\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 = 'Sales@ElyonTravel.com';
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 = 'Sales@Travel.com';// your email
    $body;
}
$success = mail($email_to, $subject, $body, 'From: <'.$email_from.'>');

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

任何其他建议都会受到赞赏,因为我不熟悉后端

1 个答案:

答案 0 :(得分: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);