Note that there are some explanatory texts on larger screens.

plurals
  1. POAjax call within php foreach loop
    primarykey
    data
    text
    <p>I am new to ajax and I am trying to implement it in my wordpress project. </p> <p>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? </p> <p>I have no idea... </p> <p>Here is what I got so far:</p> <p><strong>My wordpress loop:</strong></p> <pre><code>{foreach $posts as $item} {first}&lt;ul class="items"&gt;{/first} &lt;li class="item"&gt; [....] &lt;div class="price__tag"&gt; &lt;input type="hidden" id="price" value="FRA8" /&gt; &lt;div id="test-div"&gt;&lt;/div&gt; &lt;/div&gt; [....] &lt;/li&gt; {last}&lt;/ul&gt;{/last} {/foreach} </code></pre> <p><strong>My jQuery ajax function:</strong></p> <pre><code>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); } }); }); </code></pre> <p>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.</p> <p>I hope you did understand my problem.</p> <p>Any assistance on this is very much appreciated!</p> <p><em>EDIT</em> For completeness I am adding the wordpress function aswell:</p> <pre><code>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-&gt;CarParkAvailRate($parameters); $error = $result-&gt;Warnings-&gt;Code; if ($error == '100'){ $results = "not avail!"; } else { $results = $result-&gt;CarParks-&gt;CarParkRS-&gt;PriceCarPark-&gt;TotalPrice; } // Return String die($results); } catch(SoapFault $fault){ $fault-&gt;getMessage(); } } // create custom Ajax call for WordPress add_action( 'wp_ajax_nopriv_myAjax', 'myAjax' ); add_action( 'wp_ajax_myAjax', 'myAjax' ); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload