简体   繁体   English

从PHP响应中的元素获取数据

[英]Getting data from elements from a PHP response

I recently switched from XML to PHP for use with my simple AJAX program. 我最近从XML切换到PHP,以与我的简单AJAX程序一起使用。 However, I can't figure out how to find and pull element's data with .each like I did with the XML file format. 但是,我不知道如何像使用XML文件格式那样使用.each查找和提取元素的数据。

The HTML outputted is correct: When people type in a search, they receive a php page generated from a MySQL database. 输出的HTML是正确的:当人们键入搜索内容时,他们会收到从MySQL数据库生成的php页面。

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. 我想发生的是使用.each来获取“ div.product”,然后浏览该div中包含的元素,并使用它们的数据为我的页面添加更多的元素和样式。

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 + "<br>"); 
            });
        }
    })
}

Here is the the first two div.product from the alert generated by alert(html) 这是alert(html)生成的警报的前两个div.product

<span class="con_status">Connected successfully</span>
<div class="product">
    <div class="product_detail" data-code="HW100"></div>
    <div class="product_detail" data-name="Loaded Hardware 1&quot;"></div>
    <div class="product_detail" data-price="7"></div>
    <div class="product_detail" data-hideproduct=""></div>
    <div class="product_detail" data-stockstatus="13"></div>
    <div class="product_detail" data-productdescriptionshort=""></div>
    <div class="product_detail" data-producturl=""></div>
    <div class="product_detail" data-photourl=""></div>
</div>
<div class="product">
    <div class="product_detail" data-code="HW125"></div>
    <div class="product_detail" data-name="Loaded Hardware 1.25&quot;"></div>
    <div class="product_detail" data-price="7"></div>
    <div class="product_detail" data-hideproduct=""></div>
    <div class="product_detail" data-stockstatus="13"></div>
    <div class="product_detail" data-productdescriptionshort=""></div>
    <div class="product_detail" data-producturl=""></div>
    <div class="product_detail" data-photourl=""></div>
</div>

Edit: A problem with my ajax is it only can load the first data element. 编辑:我的ajax问题是它只能加载第一个数据元素。 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. 我不知道哪个更好,但是我使用的是正式的。

<div class="result">
    <div class="product"><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; 70mm+ wheels. Small nose and tail for manual &amp; shovits."
    data-producturl=""
    data-photourl="">
    </div></div>
</div>

Try load. 尝试加载。 For example: 例如:

$('selector for your div to parse data').load('samplepage.php #div_you_want_to load')

and for extra you can do: 另外,您可以执行以下操作:

$('selector for your div to parse data').load('samplepage.php #div_you_want_to load', function(){
//this triggers after it fetches the page
})

See http://api.jquery.com/load/ 参见http://api.jquery.com/load/

It is more flexible than .ajax with only 1 disadvantage. 它比.ajax更具灵活性,只有1个缺点。 You can not make the query synchronous if you want. 如果需要,您不能使查询同步。 But it has tons of other advantages. 但是它还有许多其他优点。

For the .each you want to do I don't see any problem with your code. 对于您要执行的.each,我的代码没有任何问题。 If the format of the output is specific you can write the code to fit that format 如果输出的格式是特定的,则可以编写适合该格式的代码

There are two problems in your JavaScript code. 您的JavaScript代码中有两个问题。

As I recall, there should be only one root element when you do something like $(html) . 我记得, 当您执行$(html)类的操作时应该只有一个根元素

One quick solution to that is to add this line 一种快速的解决方案是添加此行

html = "<div>" + html + "</div>";
$(html).find("div.product").each(function(){

Or you can change your PHP to output the result wrapped with: 或者,您可以更改PHP以输出以下结果:

<div class="result">...</div>

The success alert will then work. 然后,成功警报将起作用。

The other problem is about this line 另一个问题是关于这条线

$(this + "div.product_detail")

It should be something like 应该是这样的

$(this).find("div.product_detail")

It's not working because $(html) is returning you array as this: 它不起作用,因为$(html)返回数组如下:
[ <span class=​"con_status">​Connected successfully​</span>​, <div class=​"product">​…​</div>​, <div class=​"product">​…​</div> ​]

So you could use .filter() function like this: 因此,您可以使用.filter()函数,如下所示:
$(html).filter('.product');

Or put your response in another div like Daniel Cheng suggested. 或将您的回复放入Daniel Cheng建议的另一个div中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM