skip to Main Content

I want to use Ajax to send an id to a php file. Then I want to use the id in a php function and send the result to the main page. This has to be done by the click of a button.

My Ajax code is working when I use a fixed id, but not if it comes from a loop.

main.php

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

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

This php function that I call first. The information comes from the 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 
            }
        }

}

And the ajax.php file, where I get an error:

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 

This is the resulting error:

Error

I call another Ebay API function in ajax.php, because I need more specific information about the product.

I really don’t understand what’s happening and I can’t find anything to help me. I am sure I have a problem in the Ajax code. Could you help me find it? Thanks!

3

Answers


  1. Undefined index means $item->itemId is not defined in the $item element the loop is looking at. Is it perhaps itemID? Try a print_r() or var_dump() on each $item as the loop hits it, and examine the structure to see if itemId actually exists, and if you have a simple capitalization error.

    Login or Signup to reply.
  2. Look here

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

    Since the parameter name you pass is itemid, I suppose that this is the value you need, so, you probably need the following:

    $itemId = $item->itemid;
    

    If you are interested to know what you have in your $item, you can check it out by var_dump-ing it. You can also convert it into an array if you believe it can help you:

    json_decode(json_encode($item));
    
    Login or Signup to reply.
  3. You have not assigned any id to your input element.

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

    But you are trying to access the value through jQuery’s id selector

    var val1 = $('#itemId').val();
    

    The above returns undefined and when you add the value to your Ajax call it will be empty

    data: { itemId: val1},
    

    Therefore when you access the value in PHP you will get an Undefined index error as the value is missing from POST request

    $itemId = $_POST['itemId'];
    

    Make sure to add the id to your input so the value can be accessed. Also to prevent the error you can verify the value exists in POST using isset.

    if (isset($_POST['itemId'])) {
        $itemId = $_POST['itemId'];
    } 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search