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 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择