2016-05-05 8 views
0

Ajaxを使用してPHPファイルにIDを送信したいと考えています。次に、私はPHP関数のIDを使用し、メインページに結果を送信したいと思います。これは、ボタンをクリックするだけで実行できます。Php ForeachループとAjaxでデータを渡すことができません - Ebay APIを使用しています

固定IDを使用しているときはAjaxコードが機能していますが、ループからのものではありません。

main.php

$('#button').click(function() { 
var val1 = $('#itemId').val(); 

$.ajax({ 
    type: 'POST', 
    url: 'ajax.php', 
    data: { itemId: val1}, 
    success: function(data) { 
     $('#result').html(data); 
    } 
}); }); 

私が最初に呼び出すこのPHP関数。この情報はEbay APIからのものです。

class_ebay.php:

public function findItemsByKeywords($appId,$keyword,$sortOrder,$filterShip) 
{ 

    $apicall = $this->f_endpoint; 
    $apicall .= "OPERATION-NAME=findItemsByKeywords"; 
    $apicall .= "&SERVICE-VERSION=1.13.0"; 
    $apicall .= "&SECURITY-APPNAME=". $appId; 
    $apicall .= "&GLOBAL-ID=EBAY-FRCA"; 
    $apicall .= '&keywords='.$keyword; 
    $apicall .= '&sortOrder='.$sortOrder; 
    $apicall .= '&itemFilter(0).paramName=Currency'; 
    $apicall .= '&itemFilter(0).paramValue=CAN'; 
    $apicall .= '&itemFilter(1).name=FreeShippingOnly'; 
    $apicall .= '&itemFilter(1).value='.$filterShip; 
    $apicall .= '&itemFilter(3).name=Condition'; 
    $apicall .= '&itemFilter(3).value(0)=New'; 
    $apicall .= '&itemFilter(3).value(1)=1000'; 
    $apicall .= '&itemFilter(3).value(2)=1500'; 
    $apicall .= '&itemFilter.name=ListedIn'; 
    $apicall .= '&itemFilter.value=EBAY-ENCA'; 
    $apicall .= '&paginationInput.entriesPerPage=20'; 

    $resp = simplexml_load_file($apicall); 

    if ($resp->ack == "Success") { 

     foreach($resp->searchResult->item as $item) { 


      $pic = $item->galleryURL; 
      $link = $item->viewItemURL; 
      $title = $item->title; 
      $itemId = $item->itemId; 
      $paymentMethod = $item->paymentMethod; 
      $categoryName = $item->primaryCategory->categoryName; 
      $shipping = $item->shippingInfo->shippingType; 
      $shippingCost = $item->shippingInfo->shippingServiceCost; 
      $ConvertPrice = $item->sellingStatus->convertedCurrentPrice; 

      ?> 

       <table class='table' width='100%'> 
        <tr> 
         <td>Selection</td> 
         <td>Image</td> 
         <td>Titre</td> 
         <td>ItemId</td> 
         <td>Prix</td> 
         <td>Action</td> 
        </tr> 

        <tr> 
         <td><input type="checkbox" name="" value=""></td> 

         <td><img src=<?php echo $pic ?>></td> 
         <td><a href=<?php echo $link ?>><?php echo $title ?></a></td> 
         <td><?php echo $itemId ?></td> 
         <td><?php echo $ConvertPrice ?></td> 


         <td><button id="button"> Detail </button></td> 



        </tr> 
       </table> 

      <input type="hidden" name="itemId" value="<?php echo $itemId ?>"> 

          <div id="result" class="col-md-6"></div> 

     <?php 
      } 
     } 

} 

そして、私はエラーを取得ajax.phpファイル、:

Notice: Undefined index: itemId;

$itemId = $_POST['itemId']; 


    $apicall = "http://open.api.ebay.com/shopping?"; 
    $apicall .= "callname=GetSingleItem"; 
    $apicall .= "&version=799"; 
    $apicall .= "&appid=MingLung-research-PRD-02f839365-73d0da39"; 
    $apicall .= "&siteid=210"; 
    $apicall .= "&itemid=$itemId"; 
    $apicall .= "&responseencoding=XML"; 
    $apicall .= "&IncludeSelector=Description,Details,ItemSpecifics,ShippingCosts,TextDescription,Variations,Compatibility"; 

    $resp = simplexml_load_file($apicall); 

    $Qte = $resp->Item->Quantity; 
    $Qtevendu =$resp->Item->QuantitySold; 
    $dispo = $Qte - $Qtevendu; 
    $itemId = $resp->Item->ItemID; 

      ?> 

      <div class="panel"> 
         <ul class="nav nav-tabs" id="myTab"> 
          <li><a href="#info1" data-toggle="tab">Info 1</a></li> 
          <li><a href="#info2" data-toggle="tab">Info 2</a></li> 
          <li><a href="#info3" data-toggle="tab">Info 3</a></li> 

         </ul> 
         <div class="tab-content"> 
          <div class="tab-pane active well" id="info1"> 
           <table class='table' width='100%'> 
            <tr>    
             <td>Prix Affiché</td> 
             <td>Description</td> 
             <td>Shipping cost</td> 

            </tr> 
            <tr> 
             <td><input type="text" name="" class="form-control" value="<?php echo $resp->Item->ConvertedCurrentPrice ?>"></td> 
             <td><textarea name="" class="form-control" ><?php echo $resp->Item->Description; ?></textarea></td> 
             <td><input type="text" name="" class="form-control" value="<?php echo $resp->Item->ShippingCostSummary->ShippingServiceCost ?>"></td> 
            </tr> 
           </table> 
          </div> 
          <div class="tab-pane well" id="info2"> 
           <table class='table' width='100%'> 
            <tr>    
             <td>Type de Shipping</td> 
             <td>Etat de l'article</td> 
             <td>Quantité disponible</td> 
             <td>Localisation de l'item</td> 
            </tr> 
            <tr> 
             <td><input type="text" name="" class="form-control" value="<?php echo $resp->Item->ShippingCostSummary->ShippingType ?>"></td> 
             <td><input type="text" name="" class="form-control" value="<?php echo $resp->Item->ConditionDisplayName; ?>"></td> 
             <td><input type="text" name="" class="form-control" value="<?php echo $dispo; ?>"></td> 
             <td><input type="text" name="" class="form-control" value="<?php echo $resp->Item->Location; ?>"></td> 
            </tr> 
           </table> 
          </div> 
          <div class="tab-pane well" id="info3"> 
           <table class='table' width='100%'> 
            <tr>    
             <td>Seller Shop name</td> 
             <td>Seller Info Score</td> 
             <td>Seller PositiveFeedBack Percent</td> 
            </tr> 
            <tr> 
             <td><input type="text" name="" class="form-control" value="<?php echo $resp->Item->Storefront->StoreName; ?>"></td> 
             <td><input type="text" name="" class="form-control" value="<?php echo $resp->Item->Seller->FeedbackScore; ?>"></td> 
             <td><input type="text" name="" class="form-control" value="<?php echo $resp->Item->Seller->PositiveFeedbackPercent; ?>"></td> 
            </tr> 
           </table> 
          </div> 

         </div> 
        </div> 
      <?php 

これは結果のエラーです:

Error

I ca ajax.phpに別のEbay API関数を追加します

私は実際に何が起こっているのか分かりませんし、私を助けるものは何も見つかりません。私はAjaxコードに問題があると確信しています。それを見つけるのを助けてくれますか?ありがとう!

+0

ボタンとitemId情報が入っているHTMLはどこにありますか?arrayに変換することができます。 – RST

答えて

0

未定義インデックスとは、$ item-> itemIdがループが見ている$ item要素で定義されていないことを意味します。おそらくitemIDですか?ループがヒットするたびに$ itemごとにprint_r()またはvar_dump()を実行し、構造体を調べてitemIdが実際に存在するかどうか、および大文字化エラーが単純かどうかを調べます。あなたが渡すパラメータ名はitemidあるので

0

がここ

$apicall .= "&itemid=$itemId"; 

を見て、私は、これはあなたが必要とする値であると仮定し、そう、あなたはおそらく、次のものが必要です

$itemId = $item->itemid; 

あなたがいる場合$itemにあなたが何を持っているか知りたい場合は、var_dumpでチェックアウトできます。

json_decode(json_encode($item)); 
関連する問題