使用数组时丢失$ var的值

时间:2015-01-06 18:25:02

标签: php variables concatenation

我试图构建一个像:

的网址
https://theserver/career/pathway?path=3&edu=4&cert=

这里的首要问题是$message var在填充到数组ala $pathval时失去了$edval$certval$data的值(如如下所示)

使用上一个代码块时

else if ($action == "send") {
    //added
    #pathway var
    var_dump($_SESSION);
    $pathval = $_SESSION['pathid'];
    $edval = $_SESSION['edid'];
    $certval = $_SESSION['certid'];

    var_dump($pathval);
    var_dump($edval);
    var_dump($certval);

    $data = array(
        "path"=>$pathval,
        "edu"=>$edval,
        "cert"=>$certval
    );

    var_dump($data);


    $server_prefix = 'https://';
    $server_address = $_SERVER['SERVER_ADDR'];

    #$fullurl = $server_prefix.$server_address."/career/pathway?ed=".$edval;

    // Send the email
    $name = isset($_POST["name"]) ? $_POST["name"] : "";
    $email = isset($_POST["email"]) ? $_POST["email"] : "";
    $subject = isset($_POST["subject"]) ? $_POST["subject"] : $subject;
    $message = $server_prefix.$server_address.'/career/pathway?'.http_build_query($data);
    var_dump($message);
    // $message = $pathval;
    var_dump($message);

    $cc = isset($_POST["cc"]) ? $_POST["cc"] : "";
    $token = isset($_POST["token"]) ? $_POST["token"] : "";

    // make sure the token matches
    if ($token === smcf_token($to)) {
        smcf_send($name, $email, $subject, $message, $cc);
        echo "Your message was successfully sent.";
        // echo $name . "\n";
        // echo $email . "\n";
        // echo $subject . "\n";
    }
    else {
        echo "Unfortunately, your message could not be verified.";
    }
}

此输出(输出效果很棒!):

array(3) { ["pathid"]=> string(1) "3" ["edid"]=> string(1) "4" ["certid"]=> string(0) "" } string(1) "3" string(1) "4" string(0) "" array(3) { ["path"]=> string(1) "3" ["edu"]=> string(1) "4" ["cert"]=> string(0) "" } string(56) "https://theserver/career/pathway?path=3&edu=4&cert=" string(56) "https://theserver/career/pathway?path=3&edu=4&cert=" Your message was successfully sen

电子邮件输出Message: https://theserver/career/pathway?path=edu=cert

1 个答案:

答案 0 :(得分:0)

抱歉,我还不能发表评论。

但正如@JeremiahWinsley所说:

http://php.net/manual/en/function.http-build-query.php

$pathval = 3;
    $edval = 4;
    $certval = '';
    $server_prefix = 'https://';
    $server_address = 'website.com';
   $data = array('path'=>$pathval,
        'edu'=>$edval,
        'cert'=>$certval);

    echo $server_prefix.$server_address.'/career/pathway?'.http_build_query($data) . "\n";
    echo $server_prefix.$server_address.'/career/pathway?'.http_build_query($data, '', '&');

输出:

https://website.com/career/pathway?path=3&edu=4&cert=
https://website.com/career/pathway?path=3&edu=4&cert=

是你要的吗?

你的问题phph代码必须是这样的:

if ($action == "send") {
        // VALUES EXIST HERE
        var_dump($_SESSION);
        $pathval = $_SESSION['pathid'];
        $edval = $_SESSION['edid'];
        $certval = $_SESSION['certid'];
        var_dump($pathval);
        var_dump($edval);
        var_dump($certval);

        // << LOSING THE VALUES HERE >>
        $data = array(
            "path"=>$pathval,
            "edu"=>$edval,
            "cert"=>$certval
        );
        var_dump($data);
        // This DOES NOT work, and outputs: `path=edu=cert`
        $message = http_build_query($data);
        var_dump($message);
        // This DOES work and outputs: `3`
        $message = $pathval;
            var_dump($message);
exit;
        }

所以现在所有这些回声和你的PHP代码一切都很清楚! 请注意!

您的代码必须是:

else if ($action == "send") {
//added

$pathval = $_SESSION['pathid'];
$edval = $_SESSION['edid'];
$certval = $_SESSION['certid'];

var_dump($pathval);
var_dump($edval);
var_dump($certval);

$data = array(
    "path"=>$pathval,
    "edu"=>$edval,
    "cert"=>$certval
);

var_dump($data);

$server_prefix = 'https://';
$server_address = $_SERVER['SERVER_ADDR'];

// Send the email
$name = isset($_POST["name"]) ? $_POST["name"] : "";
$email = isset($_POST["email"]) ? $_POST["email"] : "";
$subject = isset($_POST["subject"]) ? $_POST["subject"] : $subject;
$message = $server_prefix.$server_address.'/career/pathway?'.http_build_query($data);
var_dump($message);

$cc = isset($_POST["cc"]) ? $_POST["cc"] : "";
$token = isset($_POST["token"]) ? $_POST["token"] : "";

// make sure the token matches
if ($token === smcf_token($to)) {
    smcf_send($name, $email, $subject, $message, $cc);
    echo "Your message was successfully sent.";
}
else {
        echo "Unfortunately, your message could not be verified.";
    }
}

如果有效 - 您可以删除其余的var_dump行