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


passing and receiving string variables using php

我通过URL中的链接将字符串值传递到下一个页面,如下<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();
                    }
                }
            }
        }
?>

只有在开始时,它才保持该值,如果我尝试在if (isset($_POST['submit']))中显示它,它会为计划显示空白值。不知道该怎么办。Plz建议

已编辑即使这样使用后,它也是一样的。我不知道问题出在哪里:(

    $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'];

已编辑

在ApplicationRegister.php中,我已经传递了从上一页(如)获得的隐藏值

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

然后POST方法我已经使用了这个。现在我得到了它的价值。感谢所有

已编辑

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

这是因为您没有在页面顶部调用session_start()。您需要这样才能使会话在请求之间持久存在(这是会话的要点)

除了没有调用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元素来选择计划,然后将$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();
}

看看它输出什么。

当有人点击链接时,它将正确设置变量。然而,它不会触及$_POST['submit']逻辑,因为它不是post,只是get。然后,假设您稍后实际发布到该页面,尝试访问$_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']