Php 使用ajax覆盖会话变量

Php 使用ajax覆盖会话变量,php,javascript,ajax,session-variables,Php,Javascript,Ajax,Session Variables,我已经创建了一个YouTube搜索引擎,使用会话发送视频id、标题等。我为每个按钮创建了具有唯一id的按钮,单击该按钮后,通过ajax调用一个页面,并使用该按钮的唯一id生成会话 javascript代码如下所示: <script type="text/javascript"> function loadXMLSession(videoid, videotitle) { var xmlhttp; if (wind

我已经创建了一个YouTube搜索引擎,使用会话发送视频id、标题等。我为每个按钮创建了具有唯一id的按钮,单击该按钮后,通过ajax调用一个页面,并使用该按钮的唯一id生成会话

javascript代码如下所示:

    <script type="text/javascript">
        function loadXMLSession(videoid, videotitle) {
            var xmlhttp;
            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET", "GenerateDownloadSession.php?videoid=" + videoid + "&videotitle=" + videotitle, true);
            xmlhttp.send();


            //strip off spaces and embed dashes - clean urls
            var downloadtitle = videotitle;
            downloadtitle = downloadtitle.toLowerCase();
            downloadtitle = downloadtitle.replace("-"," ");//strip off dashes with spaces
            downloadtitle = downloadtitle.replace(/ +(?= )/g,'');//replace multiple spaces with one space
            downloadtitle = downloadtitle.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');//strip off all special characters from video title
            downloadtitle = downloadtitle.replace(/ /g,"-");//replace spaces with dashes
            downloadtitle = downloadtitle.replace(/-+$|(-)+/g, '$1');//replace multiple dashes with single dash

            var url = window.location.hostname;
            url = "/development"//only for development phase
            url = url+"/download/"+downloadtitle+".html";
            window.location=url;

        }
    </script>

我遇到的问题是,当我打开浏览器后第一次单击任何下载按钮时,它工作正常。但当我再次搜索时,它将返回上一个会话值。我通过ajax调用会话函数,并向其传递值。而且,它应该覆盖会话值。但在这种情况下,它不会覆盖这些值。我如何克服这个问题?有什么建议吗?

在请求脚本时是否设置了GET值? 使用Firebug检查PHP脚本的响应。(网络->启动Ajax请求时,您将看到加载请求的文件)

At

xmlhttp.open("GET", "GenerateDownloadSession.php?videoid=" + videoid + "&videotitle=" + videotitle, true);
xmlhttp.send();
您正在执行一个异步请求,这意味着javascript在发送请求后继续,而不是等待请求完成。在呼叫更改会话数据之前,它很可能会将当前页面重定向到下载

解决方案1.)通过将最后一个参数设置为false使脚本同步

解决方案2.)将前向逻辑移到
onreadystatechange
回调函数中


在侧节点上:为什么要使用ajax来实现这一点?转发时,您可以简单地传递附加到url的参数…?

一旦收到ajax响应,您应该更改页面的位置:

xmlhttp.onreadystatechange = function() 
{
       if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          document.getElementById("myDiv").innerHTML = xmlhttp.responseText;

        var downloadtitle = videotitle;
        downloadtitle = downloadtitle.toLowerCase();
        downloadtitle = downloadtitle.replace("-"," ");//strip off dashes with spaces
        downloadtitle = downloadtitle.replace(/ +(?= )/g,'');//replace multiple spaces with one space
        downloadtitle = downloadtitle.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');//strip off all special characters from video title
        downloadtitle = downloadtitle.replace(/ /g,"-");//replace spaces with dashes
        downloadtitle = downloadtitle.replace(/-+$|(-)+/g, '$1');//replace multiple dashes with single dash

        var url = window.location.hostname;
        url = "/development"//only for development phase
        url = url+"/download/"+downloadtitle+".html";
        window.location=url;
       }
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“myDiv”).innerHTML=xmlhttp.responseText;
var downloadtitle=视频标题;
downloadtitle=downloadtitle.toLowerCase();
downloadtitle=downloadtitle.replace(“-”,“”);//用空格去掉破折号
downloadtitle=downloadtitle.replace(//+(?=)/g',);//用一个空格替换多个空格
downloadtitle=downloadtitle.replace(//[`~!@$%^&*()\ \ \ \+\-=?;:',.\{\\\[\]\\/]/gi',);//从视频标题中删除所有特殊字符
downloadtitle=downloadtitle.replace(//g,“-”);//用破折号替换空格
downloadtitle=downloadtitle.replace(//-+$|(-)+/g,$1');//用单个破折号替换多个破折号
var url=window.location.hostname;
url=“/development”//仅适用于开发阶段
url=url+“/download/”+downloadtitle+“.html”;
window.location=url;
}
}
但老实说,我不认为使用ajax然后更改实际页面的位置有什么意义。
也许你应该在另一个窗口开始下载?

将请求更改为同步不是一个好主意。同步请求完全阻塞UI线程,直到请求完成。谢谢先生:)我已经更改了想法,并使用url传递了变量,它现在正在工作。
xmlhttp.open("GET", "GenerateDownloadSession.php?videoid=" + videoid + "&videotitle=" + videotitle, true);
xmlhttp.send();
xmlhttp.onreadystatechange = function() 
{
       if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          document.getElementById("myDiv").innerHTML = xmlhttp.responseText;

        var downloadtitle = videotitle;
        downloadtitle = downloadtitle.toLowerCase();
        downloadtitle = downloadtitle.replace("-"," ");//strip off dashes with spaces
        downloadtitle = downloadtitle.replace(/ +(?= )/g,'');//replace multiple spaces with one space
        downloadtitle = downloadtitle.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');//strip off all special characters from video title
        downloadtitle = downloadtitle.replace(/ /g,"-");//replace spaces with dashes
        downloadtitle = downloadtitle.replace(/-+$|(-)+/g, '$1');//replace multiple dashes with single dash

        var url = window.location.hostname;
        url = "/development"//only for development phase
        url = url+"/download/"+downloadtitle+".html";
        window.location=url;
       }
}