doutui7955 2014-09-08 17:00
浏览 44
已采纳

传递隐藏变量没有形式和使用PHP中的链接

I want to know how to pass a (or couple) variable from one page to another in php without any form and by using a link.

I know that session is the best option but how to I implement it in the following scenario:

Delete -> process.php Add -> process.php (same page like above)

I want to pass a hidden variable that tells me what link is clicked and hence what function to call. Is there anyway not use $_GET and use sessions?!

Thanks,

  • 写回答

4条回答 默认 最新

  • dousui3124 2014-09-08 17:32
    关注

    All HTTP requests are independent of one another. Data that you have access to includes:

    1. Variables passed through the URL ($_GET)
    2. Variables posted through a form ($_POST)
    3. Cookies
    4. Session data

    Since you are opposed to using a $_GET request, and both links go to the same page, you will need some other way to determine which link is clicked. I would recommend using a form, and passing the data through a hidden input type.

    Example:

    <form action='process.php' method='post'>
       <input type='hidden' name='action' value='add' />
       <button onClick='submit();'>
          Add
       </button>
    </form>
    
    <form action='process.php' method='post'>
       <input type='hidden' name='action' value='delete' />
       <button onClick='submit();'>
          Delete
       </button>
    </form>
    
    <style type='text/css'>
    button {
       background:none!important;
       border:none; 
       padding:0!important;
       border-bottom:1px solid #444; 
       cursor: pointer;
    }
    </style>
    

    In your process.php script, you will then check against $_POST['action'] to see if it's equal to "add" or "delete". The style tag I added is something I found from SO user adardesign (*) on how to style a button like a link.

    EDIT: To answer your question on whether or not you can use sessions to accomplish this, I would say that it's possible, but probably not worth it.

    I would probably never decide to go this route, but what you could do is have an AJAX request which gets triggered when the user clicks one of those links. The AJAX request would call a PHP script which sets the session equal to "add" or "delete". In your success callback, you would then redirect the user to the process.php page, where you then be able to check the value of your $_SESSION variable. Again, it's probably not worth doing it this way, but it is possible.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度