2014-02-21 109 views
0

我试图拉每个特定类的项目的每个ID,将其传递给一个PHP脚本来处理,然后将结果返回给项目本身。似乎大部分工作,但我得到的每个项目相同的随机数。返回多个项目的AJAX结果

这里是jQuery的

function loadRenew() 
{ 
    $('.renewItem').each(function(){ 

     var currentItem = parseInt($(this).attr('id')); 

     //$('#loading').css('visibility','visible'); //show the rotating gif animation 

     $.ajax({ //create an ajax request 
       type: "POST", 
       url: "renew.php", 
       data: 'barcode='+currentItem, //with the page number as a parameter 
       dataType: "html", //expect html to be returned 
       success: function(msg){ 

        if(parseInt(msg)!=0) //if no errors 
        { 
         $("#"+currentItem).html(msg); //load the returned html 
         //$('#loading').css('visibility','hidden'); //and hide the rotating gif 
        } 
       } 

      }); 

    }); 
} 

和PHP

<?php 

if(!$_POST['barcode']) die("0"); 

$barcode = (int)$_POST['barcode']; 

echo 'barcode returned is: ' . $barcode; 

?> 

我没有收到当前条形码(CURRENTITEM)回来了,但在每个格相同的随机数。

实施例的处理结果:

<div class="panel"> 
    <div class="libcard_title"><b>Title: </b> <span style="font-family: Courier;">  Politics and cinema/Andrew Sarris. <span style="font-weight:  bold;">PN1995.9.P6&nbsp;S2</span> Stacks</span></div> 
    <div class="card_info"> 
     <b>Checked Out: </b> <span style="font-family: Courier;"> 02/05/2014</span> \ 
     <p><b>Date Due: </b> <span style="font-family: Courier;"> 05/16/2014</span></p> 
     <div class="renewItem" id="30291002497476">barcode returned is: 2147483647</div> 
    </div> 
</div> 
<div class="panel"> 
    <div class="libcard_title"><b>Title: </b> <span style="font-family: Courier;"> The   statics of particles and rigid bodies. <span style="font-weight:  bold;">QA821&nbsp;.H47</span> Stacks</span></div> 
    <div class="card_info"> 
     <b>Checked Out: </b> <span style="font-family: Courier;"> 02/06/2014</span> 
     <p><b>Date Due: </b> <span style="font-family: Courier;"> 05/17/2014</span></p> 
     <div class="renewItem" id="30291003147765">barcode returned is: 2147483647</div> 
    </div> 
</div> 
<div class="panel"> 
    <div class="libcard_title"><b>Title: </b> <span style="font-family: Courier;">  Tablets <span style="font-weight: bold;"></span> Reserve</span></div> 
    <div class="card_info"> 
     <b>Checked Out: </b> <span style="font-family: Courier;"> 02/18/2014</span> 
     <p><b>Date Due: </b> <span style="font-family: Courier;"> 02/21/2014</span></p> 
     <p><b>Fine: <span style="font-family: Courier; color:red;"> $5.00</span></b></p> 
     <div class="renewItem" id="30291009878660">barcode returned is: 2147483647</div> 
    </div> 
</div> 
<div class="panel"> 
    <div class="libcard_title"><b>Title: </b> <span style="font-family: Courier;"> Dog day afternoon [videorecording]/Warner Bros Pictures ; an Artists Entertainment Complex product <span style="font-weight: bold;">PN1997&nbsp;.D63 2006</span> Media DVD</span>  </div> 
    <div class="card_info"> 
     <b>Checked Out: </b> <span style="font-family: Courier;"> 02/05/2014</span> 
     <p><b>Date Due: </b> <span style="font-family: Courier;"> 02/12/2014</span></p> 
     <div class="renewItem" id="30291010328143">barcode returned is: 2147483647</div> 
    </div> 
</div> 
+0

你得到了什么样的随机数,什么是'currentItem'的例子? – Dutchie432

+0

从PHP中的$ barcode = ...行中删除(int)并仅回显$ barcode。不确定这是否是错误的错误原因,但这些确实是不必要的。 – lucasnadalutti

+0

有一个问题,beforesend是一个函数,但你没有做任何事情。 – Charles380

回答

1

即没有随机数。数字2,147,483,647也是计算中32位有符号整数的最大值。因此它是声明为“int”的变量的最大值。

尝试使用字符串代替。不要将它解析为一个整型值,这应该可以做到。

+0

解决!从PHP中的$ barcode中删除(int)。奇迹般有效。感谢凯尔和@lucasnadalutti! – cjp

+0

请标记为已回答:) –