Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting data from elements from a PHP response
    primarykey
    data
    text
    <p>I recently switched from XML to PHP for use with my simple AJAX program. However, I can't figure out how to find and pull element's data with .each like I did with the XML file format.</p> <p>The HTML outputted is correct: When people type in a search, they receive a php page generated from a MySQL database.</p> <p>What I want to happen is to use .each to grab a "div.product", then look through the elements contained within that div and use their data to append my page with much more elements and styling.</p> <pre><code>function htmlParser() { var search_name_field = $("input.search").val(); $.ajax({ type: "POST", url: "load_data.php?product=" + search_name_field, cache: false, dataType: "html", success: function(html){ alert(html); // This shows php response perfectly $(html).find("div.product").each(function(){ alert("success!"); // Doesn't appear var productcode = $(this + "div.product_detail").data("code"); alert(productcode); $("#output").empty(); $("#output").append("Product Code: " + productcode + "&lt;br&gt;"); }); } }) } </code></pre> <p>Here is the the first two div.product from the alert generated by <em>alert(html)</em></p> <pre><code>&lt;span class="con_status"&gt;Connected successfully&lt;/span&gt; &lt;div class="product"&gt; &lt;div class="product_detail" data-code="HW100"&gt;&lt;/div&gt; &lt;div class="product_detail" data-name="Loaded Hardware 1&amp;quot;"&gt;&lt;/div&gt; &lt;div class="product_detail" data-price="7"&gt;&lt;/div&gt; &lt;div class="product_detail" data-hideproduct=""&gt;&lt;/div&gt; &lt;div class="product_detail" data-stockstatus="13"&gt;&lt;/div&gt; &lt;div class="product_detail" data-productdescriptionshort=""&gt;&lt;/div&gt; &lt;div class="product_detail" data-producturl=""&gt;&lt;/div&gt; &lt;div class="product_detail" data-photourl=""&gt;&lt;/div&gt; &lt;/div&gt; &lt;div class="product"&gt; &lt;div class="product_detail" data-code="HW125"&gt;&lt;/div&gt; &lt;div class="product_detail" data-name="Loaded Hardware 1.25&amp;quot;"&gt;&lt;/div&gt; &lt;div class="product_detail" data-price="7"&gt;&lt;/div&gt; &lt;div class="product_detail" data-hideproduct=""&gt;&lt;/div&gt; &lt;div class="product_detail" data-stockstatus="13"&gt;&lt;/div&gt; &lt;div class="product_detail" data-productdescriptionshort=""&gt;&lt;/div&gt; &lt;div class="product_detail" data-producturl=""&gt;&lt;/div&gt; &lt;div class="product_detail" data-photourl=""&gt;&lt;/div&gt; &lt;/div&gt; </code></pre> <p>Edit: A problem with my ajax is it only can load the first data element. I fixed this by moving all the data into one single element. I could have also renamed all the elements to different classes. I don't know which is better, but I am using the formal.</p> <pre><code>&lt;div class="result"&gt; &lt;div class="product"&gt;&lt;div class="product_detail" data-code="LCSDC" data-name="Loaded Longboards - Dervish COMPLETE" data-price="240" data-hideproduct="Y" data-stockstatus="0" data-productdescriptionshort="Dervish longboard deck from Loaded Carving Systems. With a lower center of gravity and a torsionally stiff design- the Dervishes are built to hold an edge and maximize energy return. A drop-thru carver designed to work with most reverse kingpin 180mm Trucks &amp;amp; 70mm+ wheels. Small nose and tail for manual &amp;amp; shovits." data-producturl="" data-photourl=""&gt; &lt;/div&gt;&lt;/div&gt; &lt;/div&gt; </code></pre>
    singulars
    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.
    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