doulaopu2343 2011-12-23 11:12 采纳率: 100%
浏览 65
已采纳

会话变量从while循环发送特定变量

I have this simple while loop which retrieves data from a mysql query and displays several links on my homepage.

I would like to avoid using the php get function and add query strings to my urls

I am thinking of using session variables but I need help and I'm pretty sure this can't be done.

When a visitor clicks a link from the several ones displayed by the while loop, that particular variable would be set in a session.

In my code, the session will always send the last var.

Can this be done?

    <? session_start(); // Start Session Variables

    $result = mysql_query("my query");

    while($slice = mysql_fetch_assoc($result)){
        $url = $slice['url'];
        $name  = $slice['name']; ?>

        <a href="<? echo $url; ?>"><? echo $name; ?></a>

    <? } 

    $_SESSION['name'] = $name; // Store session data  ?>
  • 写回答

7条回答 默认 最新

  • duanqian2278 2011-12-26 13:33
    关注

    What you wish to do can be done by using a javascript function which will make an AJAX request sending the clicked name to the server. The server side code will then store the required session variable

     <? 
         session_start(); // Start Session Variables
    
        $result = mysql_query("my query");
        $name = '';
        while($slice = mysql_fetch_assoc($result)){
            $url = $slice['url'];
            $name  = $slice['name']; ?>
    
            <a href="<? echo $url; ?>" onclick='setSession(<? echo $url;?>);'><? echo $name; ?></a>
    
        <? } 
    

    Now the setSession will make an AJAX call passing the value obtained. This can be then saved as session url by a simple server side code

    The javascript code to present on the same page as your page having links

    <script type='text/javascript'>
        function getXMLHTTPRequest() {
        try {
        req = new XMLHttpRequest();
        } catch(err1) {
          try {
          req = new ActiveXObject("Msxml2.XMLHTTP");
          } catch (err2) {
            try {
            req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (err3) {
              req = false;
            }
          }
        }
        return req;
        }
    
    
    
          var http = getXMLHTTPRequest();
           function setSession(value) {
        var myurl = "session.php";  // to be present in the same folder
         var myurl1 = myurl;
          myRand = parseInt(Math.random()*999999999999999);
          var modurl = myurl1+"?rand="+myRand+"url"+value ; // this will set the url to be sent
    
          http.open("GET", modurl, true);
          http.onreadystatechange = useHttpResponse;
          http.send(null);
        }
    
    
        function useHttpResponse() {
           if (http.readyState == 4) {
            if(http.status == 200) {
              var mytext = http.responseText;
              // I dont think u will like to do any thing with the response
              // u can redirect the user to the req page (link clicked), once the session url has been setted
                }
             }
             else {
                 // don't do anything until any result is obtained
                }
            } 
    </script>
    

    PHP server side code to be present to set the required url as session value

    <?php
    session_start();
    if($_SESSION['url']!=""){
    unset($_SESSION['url']);   
    $_SESSION['url'] = $_REQUEST['url'];  
    }
    else {
    $_SESSION['url'] = $_REQUEST['url'];  
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号