我在 Cloud9 中执行此操作。

我的计划是制作一个图片库。

在第一页:

  • 图片很少,代表不同的“类别”。
  • 通过单击其中一个,您将移至所选类别中相册的下一页。
  • 点击相册后,您将看到该相册中的图片。

在第一页上,我创建了几个类别。当您单击其中一张图片时,应激活以下代码。代码触发,并且 jQuery 移动到下一页。

问题是,AJAX 发送了 id,但它永远不会到达另一个页面。发送的id是字符串,并且包含正确的单词。

但是,捕获 POST 的另一个页面不会收到它:变量的类型保持为 NULL,并且不包含任何内容。

首页代码(mainpage.php):

        $(document).ready(function(){ 
            getContent("mainpage.php"); 
        }); 
 
        function getContent($sivu){ 
             var xhttp = new XMLHttpRequest(); 
             xhttp.onreadystatechange = function() { 
                 if (xhttp.readyState == 4 && xhttp.status == 200) { 
                     document.getElementById("kuvaa").innerHTML = xhttp.responseText; 
                 } 
             } 
             xhttp.open("GET", $sivu, true); 
             xhttp.send(); 
        }   
 
 
         $(document).on("click", ".picture", function(){ 
 
               var pictureId = $(this).attr("name"); 
               //window.alert(jQuery.type(pictureId)); //type is string 
               //window.alert(pictureId); // content is what it should be 
 
               var request = $.ajax({ 
                   url: 'folders.php', 
                   type: 'POST', 
                   data: { 
                       'idd': pictureId 
                   }, 
 
                   success : function() { 
                      getContent("folders.php")   
                   }, 
                   error : function(err) { 
                       // in case of error 
                       console.log(err); 
                       alert('error'); 
                    }, 
                    dataType: "html" 
                }); 
            }); 

第二页(folders.php)上的变量,不会得到任何东西:

    $picId = $_POST["idd"]; 

第一页仍然正确读取folders.php(这意味着它成功发送了POST,因为成功是运行getContent 所必需的)。

欢迎任何想法。

编辑,工作解决方案:

当您点击图片时:

            $(document).on("click", ".picture", function(){ 
                var pictureId = $(this).attr("name"); 
                getNewContent(pictureId); 
 
            }); 

这就是:

            function getNewContent($idd){ 
                var xhttp = new XMLHttpRequest(); 
                xhttp.onreadystatechange = function() { 
                    if (xhttp.readyState == 4 && xhttp.status == 200) { 
                        document.getElementById("kuvaa").innerHTML = xhttp.responseText; 
                    } 
                } 
            xhttp.open("GET", "/folders.php?idd="+$idd, true); 
            xhttp.send(); 
            }  

请您参考如下方法:

我建议您通过查询字符串将参数从页面传递到其他页面,如果您的页面不需要后端信息,您可以使用Javascript捕获该值,如果不需要,您可以根据Hachachin的答案获取该值。

/phpscript.php?idd=151 

使用此脚本,您可以从查询字符串中获取 idd

function getParameterByName(name, url) { 
    if (!url) url = window.location.href; 
    name = name.replace(/[\[\]]/g, "\\$&"); 
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)", "i"), 
        results = regex.exec(url); 
    if (!results) return null; 
    if (!results[2]) return ''; 
    return decodeURIComponent(results[2].replace(/\+/g, " ")); 
} 
 
var id = getParameterByName('idd'); 


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!