Session变量从while循环发送特定的变量 [英] Session variables sending specific variable from a while loop

查看:130
本文介绍了Session变量从while循环发送特定的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的 while循环从MySQL查询中检索数据并显示在我的主页几个环节。

我想避免使用PHP get函数,并添加查询字符串,以我的网址

我想使用会话变量,但我需要帮助,我pretty的肯定,这无法做到的。

当访问者点击从几个所得由while循环显示的链接,该特定变量将在会议上进行设置。

在我的code,会话将始终发送的最后一个变种。

可以这样做?

 &LT ;?在session_start(); //开始会话变量

    $结果= mysql_query(我的查询);

    而($片= mysql_fetch_assoc($结果)){
        $ URL = $片['URL'];
        $名称= $片['名称']; ?>

        < A HREF =&LT ;?回声$网址;>中>&LT ;?回声$名称; ?>&所述; / a取代;

    &LT ;? }

    $ _SESSION ['名称'] = $名称; //存储会话数据&GT?;
 

解决方案

你希望怎么做可以使用JavaScript函数,这将使一个AJAX请求发送点击名称服务器来完成。服务器端code然后将存储所需的会话变量

 &LT ;?
     在session_start(); //开始会话变量

    $结果= mysql_query(我的查询);
    $ NAME ='';
    而($片= mysql_fetch_assoc($结果)){
        $ URL = $片['URL'];
        $名称= $片['名称']; ?>

        < A HREF =&LT ;?回声$网址;>中的onclick ='setSession(LT ;?回声$网址;?>),'>&LT ;?回声$名称; ?>&所述; / a取代;

    &LT ;? }
 

现在的setSession将使一个AJAX调用传递得到的值。这可以再保存为会议URL通过一个简单的服务器端code

JavaScript的code至present在同一页上您有网页链接

 <脚本类型=文/ JavaScript的'>
    功能getXMLHTT prequest(){
    尝试 {
    REQ =新XMLHtt prequest();
    }赶上(ERR1){
      尝试 {
      REQ =新的ActiveXObject(MSXML2.XMLHTTP);
      }赶上(ERR2){
        尝试 {
        REQ =新的ActiveXObject(Microsoft.XMLHTTP);
        }赶上(ERR3){
          REQ = FALSE;
        }
      }
    }
    返回REQ;
    }



      VAR HTTP = getXMLHTT prequest();
       功能setSession(值){
    VAR myurl =session.php文件; //为present在同一个文件夹中
     VAR myurl1 = myurl;
      myRand = parseInt函数(的Math.random()* 999999999999999);
      VAR modurl = myurl1 + myRand +URL+价值兰特=?; //这将设置的URL发送

      http.open(GET,modurl,真正的);
      http.onreadystatechange = useHtt presponse;
      http.send(空);
    }


    功能useHtt presponse(){
       如果(http.readyState == 4){
        如果(http.status == 200){
          VAR MYTEXT = http.responseText;
          //我不觉得你会喜欢做的任何事情,与响应
          // U可以将用户重定向到REQ页面(点击链接),一旦会话URL已经设置好的
            }
         }
         其他 {
             //直到获得任何结果,没有做任何事情
            }
        }
< / SCRIPT>
 

PHP服务器端code是present设置所需的URL作为会话值

 < PHP
在session_start();
如果($ _ SESSION [网址]!=){
取消设置($ _ SESSION ['URL']);
$ _SESSION [网址] = $ _REQUEST ['URL'];
}
其他 {
$ _SESSION [网址] = $ _REQUEST ['URL'];
}
?>
 

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  ?>

解决方案

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'];  
}
?>

这篇关于Session变量从while循环发送特定的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆