简体   繁体   English

Php 位置 Header - 变量

[英]Php Location Header - Variable

I need some help with header location using variable.我需要一些有关使用变量的 header 位置的帮助。 I loose my variable.我失去了我的变量。

How do I keep my $var?如何保留我的 $var?

    $var = $_GET['valueToSearch']
    (I get the correct value here)

if(isset($_POST['shipping']))
header("Location: shipping.php?valueToSearch=".$var);
exit;
}

This takes me to shipping, but I loose the $var value.这带我去发货,但我丢失了 $var 值。

if(isset($_POST['shipping']))
$var = 'C2234';
header("Location: shipping.php?valueToSearch=".$var);
exit;
}

This works correctly.这工作正常。

How do I keep $var or any other suggestions?我如何保留 $var 或任何其他建议?

I also tried:我也试过:

window.location = "shipping.php?valueToSearch=" + $var;

Thanks谢谢

- More detail:

<?php
$var = $_GET['valueToSearch']; 
$query = "SELECT increment_id, status, created_at, method, shipping_description, company, name, telephone, email, suburb, state, country, data_transf_ordervalue_incl, DATE_FORMAT(created_at, '%b %y')AS date FROM tblorder WHERE increment_id = '$var' order by created_at DESC";
    require_once('/../../private/dbcon_order.php');
    $result = mysqli_query($connect, $query) or die ('Error');
    $row_cnt = mysqli_num_rows($result);

if(isset($_POST['shipping']))
{
header("Location: shipping.php?valueToSearch=".$var);
exit;


I'm not 100% sure what you're asking, but I'm answering based on "when user make a $_POST['shippping'], how do I get the $_POST['shipping'] value after page redirection "我不是 100% 确定你在问什么,但我的回答是基于“当用户进行 $_POST['shippping'] 时,我如何在页面重定向后获取 $_POST['shipping'] 值”

$shipping = "";
// Assuming that "shipping" is a string type. 
if (isset($_POST["shipping"])) {
    $shipping = filter_input(INPUT_POST, "shipping", FILTER_DEFAULT);
} elseif ($_GET["shipping"])) {
    $shipping = filter_input(INPUT_GET, "shipping", FILTER_DEFAULT);
}


// Debug
echo "Your shipping value is : $shipping";

header("Location: shipping.php?valueToSearch=$var&shipping=$shipping");

So, your retained value is accessible with $_GET['shipping'] after the $_POST["shipping"] redirection.因此,在 $_POST["shipping"] 重定向之后,可以使用 $_GET['shipping'] 访问您的保留值。

Another way to store variable despite page redirection is by using $_SESSION.另一种在页面重定向的情况下存储变量的方法是使用 $_SESSION。 To do this, you can do such.为此,您可以这样做。

// To set a session value
$_POST["shipping"] = $_SESSION["shipping"];
// To get a session value
$shipping = $_SESSION["shipping"];
// To remove a session value
unset($_SESSION["shipping"]);

Another way to store variable despite page redirection is using user browser cookies, but the cookie is typically reserved for authentication purposes only, you can look it up here if you're interested.另一种存储变量尽管页面重定向的方法是使用用户浏览器 cookies,但 cookie 通常仅用于身份验证目的,如果您有兴趣,可以在此处查找。 https://www.php.net/manual/en/features.cookies.php https://www.php.net/manual/en/features.cookies.php

Thanks for all your help.感谢你的帮助。

I found this article: $_GET or $_SESSION for passing variable我找到了这篇文章: $_GET or $_SESSION for passing variable

I used a hidden field (increment) and then used $_SESSION to pass it between pages:我使用了一个隐藏字段(增量),然后使用 $_SESSION 在页面之间传递它:

Set value:设定值:

$_SESSION['var'] = $_POST['increment'];

and on 2nd page: Get the value:在第二页:获取值:

$valueToSearch = $_SESSION['var'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM