douxiaomang5640 2016-01-06 01:50
浏览 33
已采纳

如何将PHP对象从一个页面传递到下一个页面两次?

$ID= $_GET['id'];

<form method="post" action="updateorder.php?id=?"<?php echo htmlspecialchars($ID);?>">

I am using PHP and I can pass through the ID from the index.php to this page (openorders). I would like to go from this (openorders.php) to another page to update my orders - updateorder.php. I have found how to pass it through once, but not one more time after that. How would you do this? Thank you.

  • 写回答

2条回答 默认 最新

  • doumao6212 2016-01-06 02:38
    关注

    As stated, use sessions and as an example.

    Sidenote: Consult my "Edit" further below about ?id=?" in your form.

    • Assign a session array to the GET array, then use the same session array for subsequent pages.

    The variable names don't really matter, it's the session array that is important here.

    First start with, and from the page where you will retrieve the GET array:

    session_start();
    
    if(!empty($_GET['id'])){
       $ID = $_GET['id'];
    }
    
    $_SESSION['id'] = $_GET['id'];
    
    $var_page1 = $_SESSION['id'];
       echo "ID #: " . $var_page1;
    

    then on the next page:

    session_start();
    
    if(isset($_SESSION['id'])){
    
       $var_page2 = $_SESSION['id'];
       echo "ID #: " . $var_page2;
    
    }
    

    and the subsequent page following that:

    session_start();
    
    if(isset($_SESSION['id'])){
    
       $var_page3 = $_SESSION['id'];
       echo "ID #: " . $var_page3;
    
    }
    

    References:


    Sidenote: If your GET array will always be an integer, you can pass (int) to it for more protection.

    $ID = (int)$_GET['id'];
    

    Reference:


    Edit: (something I noticed while testing) - I used 123 as an example, being:

    $_GET['id'] = 123;
    $ID = (int)$_GET['id'];
    

    Your form will need to be adjusted because of ?id=?". (the extra ? and quote).

    <form method="post" action="updateorder.php?id=?"<?php echo htmlspecialchars($ID);?>">
                                                   ^^ extra question mark and quote - delete it
    

    Because that will translate to the following (and in HTML source) which is a ? and adding an additional quote, which is something you don't want and will fail you.

    <form method="post" action="updateorder.php?id=?"123">
    

    It will need to be changed to the following:

    <form method="post" action="updateorder.php?id=<?php echo htmlspecialchars($ID);?>">
    

    which will produce the following in HTML source:

    <form method="post" action="updateorder.php?id=123">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题