![](/img/trans.png)
[英]How to receive jQuery post response before completing execution of PHP script?
[英]PHP Script Does not Receive jQuery POST
首先,我是一個jQuery noob和一個總的PHP菜鳥所以我很抱歉,如果這是一個愚蠢的問題(雖然我已經搜索了互聯網,特別是,這個網站上下不知道我是什么我我做錯了。 此外,如果你看到任何其他錯誤/凌亂的代碼等,請不要猶豫告訴我:)。
我正在嘗試創建一個庫,我有一個前一個按鈕和一個下一個按鈕。 當您單擊上一個時,img-tag的src-attribute將通過jquery更改為上一個圖像,並與下一個buton和下一個圖像相同。 但是,當您按下上一個按鈕並且當前圖像是第一個圖像時,我希望它返回到最后一個圖像。 我需要這個動態發生,因為我有幾個這些畫廊中的圖片總數不同。 現在,我目前的方法是(大致):
從單擊的按鈕發送方向參數到我的圖像切換功能(“向后”或“向前”)
提取圖像源的文件路徑並將其發布到外部PHP腳本,然后計算該目錄中的文件數。
檢查方向參數是否向前。 如果是,請將當前圖像文件名末尾的數字增加1。 如果不是,則減去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
這條線:
$imgStr = $id1;
應該:
$imgStr = $_POST['id1'];
vardump($_POST)
檢查數組上的所有值。 如果只有一個空數組,您的頁面不會發布任何內容,否則它將輸出您帖子的所有內容。 然后開始解決這個問題:檢查所有存在的索引並在代碼中替換它們;)
好的,所以我仍然不知道出了什么問題,但我只是重寫了我的jQuery腳本的POST / GET部分,以及從頭開始的PHP腳本,現在它正在工作。 這是我的新代碼,以防將來有人再次發現這個問題,並遇到類似的問題。 我用這個替換了POST - $ .ajax函數(同時也改變了我jQuery其余部分中的一些變量,所以不要讓你混淆):
$.post( "test3.php", {a: imageStringSlice}, function(data) {
//alert(data);
count = data - 2;
});
這個$ .get:
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幫助我:)!
我對這個問題已經很晚了,但是我最近在同一個問題上遇到了困難,在任何地方都找不到任何幫助。
簡單的答案是,如果PHP無法識別表單,PHP不會填充$ _POST,但輸入仍然可用。
當我通過javascript發布(在我的情況下json數據)時,我通過使用這個從PHP獲取它:
$postbody = file_get_contents("php://input");
請注意,它不等同於帖子,因為它未被處理,因此您可能需要對此進行大量工作。
有關流包裝器的更多信息,請點擊此處:[php php on php input] [ http://php.net/manual/en/wrappers.php ]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.