简体   繁体   中英

Php Location Header - Variable

I need some help with header location using variable. I loose my variable.

How do I keep my $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.

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?

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 "

$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.

Another way to store variable despite page redirection is by using $_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. https://www.php.net/manual/en/features.cookies.php

Thanks for all your help.

I found this article: $_GET or $_SESSION for passing variable

I used a hidden field (increment) and then used $_SESSION to pass it between pages:

Set value:

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

and on 2nd page: Get the value:

$valueToSearch = $_SESSION['var'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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