PHP脚本不接收jQuery POST

时间:2014-07-14 01:53:00

标签: php jquery ajax image post

首先,我是一个jQuery noob和一个总的PHP菜鸟所以我很抱歉,如果这是一个愚蠢的问题(虽然我已经搜索了互联网,具体来说,这个网站上下都无法计算我做错了什么。此外,如果您发现任何其他错误/杂乱的代码等,请不要犹豫告诉我:)。

我正在尝试创建一个库,其中包含上一个和下一个按钮。当您单击上一个时,img-tag的src-attribute将通过jquery更改为上一个图像,并与下一个buton和下一个图像相同。但是,当您按下上一个按钮并且当前图像是第一个图像时,我希望它返回到最后一个图像。我需要这个动态发生,因为我有几个这些画廊中的图片总数不同。现在,我目前的方法是(大致):

  • 从单击的按钮发送方向参数到我的图像切换 功能(“向后”或“向前”)

  • 提取图像源的文件路径并将其发布到外部 PHP脚本然后计算其中的文件数 。目录

  • 检查direction参数是否为forward。如是, 增加当前图像文件名末尾的数字 一个人。如果不是,则减去1(导致不存在的文件 名称)。此新文件名将分配给nextImage变量。

  • 检查nextImage是否存在。如果是,请将其指定为 img src-attribute。如果没有,请从外部获取文件计数 PHP脚本,将其作为文件编号实现到nextImage中 然后将其分配给src-attribute。

大部分都有效。但是,在第一张图像上单击上一张图像时获取最后一张图像则不会。我做了一些调试,并将其缩小到似乎停止工作的程度。 Firebug告诉我jQuery POST功能正常工作(我也得到了成功警报)并且它将我想要的数据发布到PHP文件中。但是,在PHP脚本中,永远不会输入if (isset($_POST["id1"]))分支。它总是直接进入else分支,即使我基本上从本网站上接受的答案中复制并粘贴了整个脚本。

这是jQuery代码:

var nextImageLength = " ";
var nextImage = " ";
var imageString = " ";
var nextImageStr = " ";
var nextImageNmb = 0;

var countImageLength = function($countimagelength) {
    nextImageLength = nextImageStr.length;
    if (nextImageLength < 3) {
        if (nextImageLength < 2) {
            nextImage = imageString + '00' + nextImageStr + '.jpg';
        }
        else {
            nextImage = imageString + '0' + nextImageStr + '.jpg';
        }
    }
    else {
        nextImage = imageString + nextImageStr + '.jpg';
    }
}

function multiPageToggle(direction) {
    var currentImage = $(".viewport_img_high").attr("src");
    imageString = currentImage.slice( 0, 35 );
    $.ajax(
            {
            type: "POST",
                url: "countImages.php",

                data: { id1: imageString},
                success: function (count) {
                    //alert('success');

            }
    });     
    var currentImageNmb = currentImage.slice(-7, -4);
    if (direction == "forward") {
        nextImageNmb = (parseInt(currentImageNmb)) + 1;
    }
    else {
        nextImageNmb = (parseInt(currentImageNmb)) - 1;
    }
    nextImageStr = nextImageNmb.toString();
    countImageLength();
    $.get(nextImage)
            .done(function() { 
                $(".viewport_img_high").attr("src", nextImage);
         }).fail(function() { 
                 if (nextImageNmb < 1) {
                $.get('countImages.php', function(count) {
                    nextImageStr = count;
                });
                countImageLength();
                $(".viewport_img_high").attr("src", nextImage);
            }
            else {
                nextImageStr = "1";
                countImageLength();
                $(".viewport_img_high").attr("src", nextImage);
            }
         });
}   

这是我的PHP脚本:

<?php
if (isset($_POST["id1"])) 
{
  $imgStr = $id1;
} 
else 
{
  $imgStr = "1";
  echo "Oops. Something went really wrong here.";
}
$imagePath = "c:/xampp/htdocs/portfolio/images2/" + $imgStr + "/";
$count = iterator_count(new DirectoryIterator($imagePath));
echo $count;
?>

编辑:我将在下面添加一些Firebug数据。也许这可以帮助理解这一点。 响应标题:

Connection  Keep-Alive
Content-Length  77
Content-Type    text/html
Date    Tue, 15 Jul 2014 04:26:24 GMT
Keep-Alive  timeout=5, max=100
Server  Apache/2.4.7 (Win32) OpenSSL/1.0.1e PHP/5.5.9
X-Powered-By    PHP/5.5.9

请求标题:

Accept  */*
Accept-Encoding gzip, deflate
Accept-Language de,en-US;q=0.7,en;q=0.3
Cache-Control   no-cache
Connection  keep-alive
Content-Length  43
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
Host    localhost
Pragma  no-cache
Referer http://localhost/portfolio/index_b.php
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0
X-Requested-With    XMLHttpRequest

响应:

array(1) {
  ["id1"]=>
  string(35) "images2/des004_large_/des004_large_"
}
0

4 个答案:

答案 0 :(得分:3)

该行:

$imgStr = $id1;

应该是:

$imgStr = $_POST['id1'];

答案 1 :(得分:1)

vardump($_POST)检查数组上的所有值。如果只有一个空数组,您的页面不会发布任何内容,否则它将输出您帖子的所有内容。然后开始解决这个问题:检查所有存在的索引并在代码中替换它们;)

答案 2 :(得分:0)

好的,所以我仍然不知道出了什么问题,但我只是重写了我的jQuery脚本的POST / GET部分,以及从头开始的PHP脚本,现在它正在工作。这是我的新代码,以防将来有人再次发现这个问题,并遇到类似的问题。我用这个替换了POST - $ .ajax函数(同时也改变了我jQuery其余部分中的一些变量,所以不要让你感到困惑):

$.post( "test3.php", {a: imageStringSlice}, function(data) {
            //alert(data);
    count = data - 2;
});

$ .get with this:

nextImageStr = count.toString();
countImageLength();
$(".viewport_img_high").attr("src", nextImage);

PHP脚本:

<?php
if( $_REQUEST["a"] )
{
    $imgstr = $_REQUEST['a'];
    settype($imgstr, "string");
    $count = iterator_count(new DirectoryIterator($imgstr));
    echo $count;
}
else {
    echo "1";
}
?>

感谢Barmar,尤其感谢gcarvalho97帮助我:)!

答案 3 :(得分:0)

我对这个问题已经太迟了,但是我最近在同一个问题上遇到了困难,在任何地方找不到任何帮助。

简单的答案是,如果PHP无法识别表单,PHP不会填充$ _POST,但输入仍然可用。

当我通过javascript发布(在我的情况下json数据)时,我通过使用这个从PHP获取它:

$postbody = file_get_contents("php://input");

请注意,它不等同于帖子,因为它未经处理,因此您可能需要对此进行大量工作。

有关流包装器的更多信息,请访问:[php php on php input] [http://php.net/manual/en/wrappers.php]