dsa45664 2013-12-18 22:30
浏览 99

在php foreach循环中调用Ajax

I am new to ajax and I am trying to implement it in my wordpress project.

The ajax call is working fine but the problem is that the ajax result is only showing for the first item in the foreach loop. I guess I am missing an iteration or $.each loop within the jQuery function?

I have no idea...

Here is what I got so far:

My wordpress loop:

{foreach $posts as $item}
{first}<ul class="items">{/first}
<li class="item">
    [....]
        <div class="price__tag">
            <input type="hidden" id="price" value="FRA8" />
            <div id="test-div"></div>
        </div>
    [....]
</li>
{last}</ul>{/last}
{/foreach}

My jQuery ajax function:

jQuery(document).ready(function() {
var price= jQuery("#price").val();

    jQuery.ajax({
        type: 'POST',
        url: 'http://DOMAIN.COM/wp-admin/admin-ajax.php',
        data: {
            action: 'myAjax',
            greeting: price,
        },
        success: function(data, textStatus, XMLHttpRequest){
            jQuery("#test-div").html('');
            jQuery("#test-div").append(data);
        },
        error: function(MLHttpRequest, textStatus, errorThrown){
            alert(errorThrown);
        }
    });
});

Some background information - the result is coming from a SOAP API request and is posting the price for a specific product which is beeing displayed in the wordpress loop.

I hope you did understand my problem.

Any assistance on this is very much appreciated!

EDIT For completeness I am adding the wordpress function aswell:

function add_myscript(){
    wp_enqueue_script( 'myajax.js', get_bloginfo('template_directory') . "/Templates/content/myajax.js", array( 'jquery' ) );
}
add_action( 'init', 'add_myscript' );
function myAjax(){

[…SOAP CLIENT…]

        $result = $soapClient->CarParkAvailRate($parameters);
        $error = $result->Warnings->Code;
        if ($error == '100'){
            $results = "not avail!";
        }
        else {
            $results = $result->CarParks->CarParkRS->PriceCarPark->TotalPrice;
        }
    // Return String
    die($results);
}
catch(SoapFault $fault){
$fault->getMessage();
}
}

// create custom Ajax call for WordPress
add_action( 'wp_ajax_nopriv_myAjax', 'myAjax' );
add_action( 'wp_ajax_myAjax', 'myAjax' );
  • 写回答

2条回答 默认 最新

  • douguyi3903 2013-12-18 22:35
    关注

    The "id" values for elements must be unique. Each item in your list has a <div> element with an "id" of "test-div".

    These two elements get repeated for each iteration of the loop:

    <input type="hidden" id="price" value="FRA8" />
    <div id="test-div"></div>
    

    You need them to have unique id values for each iteration.

    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面