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条)

报告相同问题?

悬赏问题

  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!