使用php传递和接收字符串变量

时间:2012-07-27 01:57:34

标签: php html

我将字符串值通过网址中的链接传递到下一页,如<a href="ApplicationRegister.php?plan=trial"> 在ApplicationRegister.php页面中,我得到的值如下$plan = $_GET["plan"]; 我会把它放到像$_SESSION['plans'] = $plan;这样的会话变量中 在这里,我得到了价值。但在if语句之后,即使在使用Session变量之后,我也没有得到该计划的值。

我的完整代码就像这样

 $plan = $_GET["plan"];
    echo $plan;
    $_SESSION['plan'] = $plan;
$plans = $_SESSION['plan'];
    echo $_SESSION['plans'];

    include('connect.php');


        If (isset($_POST['submit']))
        {
            $CompanyName = $_POST['CompanyName'];

            $CompanyEmail = $_POST['CompanyEmail'];
            $CompanyContact = $_POST['CompanyContact'];
            $CompanyAddress = $_POST['CompanyAddress']; 
            $StoreName = $_POST['StoreName'];
            echo $plans;

      $myURL ="$_SERVER[HTTP_HOST]";
                $myURL =$StoreName.'.'.$myURL;

        if (stripos($myURL, 'www.') !== 0) {
           $myURL = 'www.' . $myURL;

        }
        if (stripos($myURL, 'http://') !== 0) {
           $myURL = 'http://' .$myURL;

        }

        if(stripos($myURL, '.com') !== 0) {
            $myURL = $myURL . '.com';

        }
        echo $plans;

            $RegistrationType = $_POST['RegistrationType'];

            $Status = "Active";
            $sql = "select * from plans where planname = '$plans'";
            echo $sql;
            mysql_query($sql) or die (mysql_error());
            $planID = $row['planid'];


            $query1 = "select count(CompanyEmail) from ApplicationRegister where CompanyEmail = '$CompanyEmail'" ;

            $result1 = mysql_query($query1) or die ("ERROR: " . mysql_error());

            $msg = "";
             while ($row = mysql_fetch_array($result1))
             {

                if($row['count(CompanyEmail)'] > 0)
                {
                    $msg = "<font color='red'> <b>This E-mail id is already registered </b></font> ";
                    break;
                }
            }
            if($msg == "")
            {


                $query2 = "select count(URL) from ApplicationRegister where URL = '$myURL' ";
                $result2 = mysql_query($query2) or die ("ERROR: " . mysql_error());
                $msg = "";
                while ($row = mysql_fetch_array($result2))
                {

                    if($row['count(URL)'] > 0)
                    {
                        $msg = "<font color='red'> <b>This Stroename is already registered </b></font> ";
                        break;
                    }
                }
                if($msg == "")
                {
                    $sql = "INSERT INTO ApplicationRegister(planid, CompanyName, CompanyEmail, CompanyContact, CompanyAddress, RegistrationType,                        ApplicationPlan, ApplicationStatus, URL, CreatedDate) VALUES ('$planID', '$CompanyName', '$CompanyEmail', '$CompanyContact',                    '$CompanyAddress', '$RegistrationType', '$plans', '$Status', '$myURL', NOW() )";

                    mysql_query($sql) or die(mysql_error());
                    $id = mysql_insert_id();
                    $_SESSION['application_id'] = $id;

                    if($plans == "trail")
                    {
                        header("Location: userRegister.php");
                        exit();
                    } 
                    else
                    {
                        header("Location : PaymentGateway.php");
                        exit();
                    }
                }
            }
        }

?>

只有在开头它保留了值,如果我尝试在(isset($_POST['submit']))内显示它,它会显示计划的空白值。不知道该干什么。 Plz建议

EDITED 即使在使用这样之后,它也一样。我不知道可能是什么问题:(     

    $plan = $_GET["plan"];
    echo $plan;
    $_SESSION['plans'] = $plans;
    echo $_SESSION['plans'];
  // $plan = +$plan; 
    include('connect.php');


        If (isset($_POST['submit']))
        {
            $CompanyName = $_POST['CompanyName'];

            $CompanyEmail = $_POST['CompanyEmail'];
            $CompanyContact = $_POST['CompanyContact'];
            $CompanyAddress = $_POST['CompanyAddress']; 
            $StoreName = $_POST['StoreName'];
            echo $_SESSION['plans'];

EDITED

在ApplicationRegister.php中,我已经通过了隐藏值,这是我之前的页面

<input type="hidden" name="plan" value="<?php echo $plan ?>"/>

然后POST方法我用过这个。现在我正在为它获得价值。感谢所有

EDITED

if($PlanName == "trail")
                    {

                        header("Location: userRegister.php");
                        exit();
                    } 
                    else
                    {
                        header("Location : PaymentGateway.php");
                        exit();
                    }

3 个答案:

答案 0 :(得分:3)

这是因为您没有在页面顶部调用session_start()。您需要将会话保持在请求之间(这是会话点)

答案 1 :(得分:0)

当有人点击链接时,它会正确设置变量。但是,它不会达到$_POST['submit']逻辑,因为它不是一个帖子,只是一个获取。然后,假设您稍后实际发布到该页面,尝试访问$_GET中的任何内容将为null,然后将会话变量重置为null。

你的第一页应该有类似这样的代码

<form action="ApplicationRegister.php" method="post">
    <select name="plan">
        <option value="trial">Trial</option>
    </select>
<input type="submit"/>
</form>

然后,您检查$_POST['plan']$_POST['submit']

答案 2 :(得分:0)

除了不调用session_start();之外,这段代码错了:

$plan = $_GET["plan"];
echo $plan;
$_SESSION['plan'] = $plan;
$plans = $_SESSION['plan'];
echo $_SESSION['plans'];

应该是:

$plan = $_GET["plan"];
echo $plan;
$_SESSION['plan'] = $plan;
$plans = $_SESSION['plans'];
echo $_SESSION['plans'];

设置 $_SESSION['plan'],然后尝试访问 $_SESSION['plans']

另外,您是点击链接还是提交form?您说您有一个链接,但您的代码尝试访问从form传递的值。

如果您使用的是form,请勿使用链接。而是使用select element选择一个计划,然后将$plan = $_GET["plan"];更改为$plan = $_POST["plan"];

修改

对于重定向问题,请尝试以下代码:

echo "<pre>** Plan Name: **\n";
var_dump($PlanName);
echo "</pre>";
if($PlanName == "trail")
    {
        header("Location: userRegister.php");
        exit();
    } 
    else
    {
        header("Location: PaymentGateway.php");
        exit();
}

并查看它输出的内容。