dtbrd80422
dtbrd80422
2017-04-27 18:58

将从函数调用中获取的数据传递给第二个PHP页面

已采纳

As a result of discussion on my previous question:

Passing data to another page after form submit with PHP

I've discovered my syntax is correct but not working the way I need it.

I'm trying to pass a variable from my first page (my page with a form) to my second page (basically just a splash to say they submitted the form).

The variable is a unique ID used as a reference number for the submission. I get the unique ID by generating a random sequence in a function.

My problem is such: I can get my variable to pass across the two pages with this code

//(First Page)
$result = "TEST";
header('Location: submittedApplication.php?result='. $result);

and I grab it with this code

//(Second Page)
echo $_GET['result'];

Which works and passes a variable for me. But what I need is to utilize this function:

    function gen_uid($l=10)
        { 
        $prefix = "#####";
        $str = ""; 
        date_default_timezone_set('America/New_York');
        $date = date("Y.m.d");

        for ($x=0;$x<$l;$x++)
        $str .= substr(str_shuffle("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 1);
        echo $prefix . $str . "<br/>" . "Generated On: " . $date; 
        }

What I would like to use is

$result = gen_uid();

But it just passes empty to the second page, even though when I echo it out to test on the first page it works and gives me data like this

######NMB32R3MVQ
Generated On: 2017.04.27

As a recap; Page 1 is: form.php, Page 2 is: submittedApplication.php, I'm trying to pass $result (while holding the string my function generates) from page 1 -> page 2. I'm new to web development so I'm open to any avenue of getting this to work, I'm just not able to determine what avenue that is.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • duanchi0883649 duanchi0883649 4年前

    $result is giving a null value because you're not returning anything from the function gen_uid(), you're only echoing things.

    点赞 评论 复制链接分享
  • douhan8610 douhan8610 4年前

    Function you have created is not returning the desired result its just displaying it. Replace:

    echo $prefix . $str . "<br/>" . "Generated On: " . $date;

    with

    $final_result = $prefix . $str . "<br/>" . "Generated On: " . $date;
    return $final_result; 
    

    this should resolve your problem.

    点赞 评论 复制链接分享