简体   繁体   English

在while循环外回显php数组结果-使用jquery更新html div

[英]Echo php array result outside while loop - to update html div using jquery

Ok so this one has been racking my brain. 好的,这已经让我绞尽脑汁了。 I have the following page product_main.php . 我有以下页面product_main.php This is my main product display page for my application. 这是我的应用程序的主要产品显示页面。 I am using the following code to display products based on a specific category link on another page. 我正在使用以下代码在另一页上基于特定类别的链接显示产品。 This works fine and displays the products based on their category: 这可以正常工作并根据其类别显示产品:

<?php

    $q = $_GET['cat'];

    include 'db_connect.php';

    $result = mysqli_query($con, "SELECT * FROM product_info WHERE ProductCat= '".$q."'");

    while($row = mysqli_fetch_array($result)) {

        $pro_id = $row['ProductID'];
        $pro_tit = $row['ProductTitle'];
        $pro_des = $row['ProductDesc'];
        $pro_img = $row['ProductImg'];

        echo "

        <div class='product-display'>
            <img id='prodImage' src='Images/$pro_img.jpg' alt='product photo'>
            <h3>$pro_tit</h3>
            <h3>$pro_des</h3>
        </div>

        ";

    }
?>

Now what I want to do is add a filter drop down to select a different category whilst on the same page. 现在,我想做的是在同一页面上添加一个过滤器下拉列表以选择其他类别。 I have the following code to grab the output of function_load_product.php and update the div element on my page: 我有以下代码来获取function_load_product.php的输出并更新页面上的div元素:

<script>

function selectCat(str) {
if (str == "") {
    document.getElementById("prodArea").innerHTML = "";
    return;
} else { 
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("prodArea").innerHTML = this.responseText;
        }
    };

    xmlhttp.open("GET","function_load_product.php?cat="+str,true);
    xmlhttp.send();
  }
}

</script>

This again works fine at 'attempting' to grab the output from function_load_product.php . 这在“尝试”以从function_load_product.php获取输出时也可以正常工作

I thought that using the same code as per the main display page - I would get the same functionality - but I seem to be having trouble getting anything to display. 我以为使用与主显示页面相同的代码-我将获得相同的功能-但似乎无法显示任何内容。

I think I have narrowed it down to a scope issue - with the Jquery not returning anything as the echo statement is contained within the while loop. 我认为我已将其范围缩小到一个范围问题-由于echo语句包含在while循环中,因此Jquery不返回任何内容。

I have attempted the following code as this did work for me on another project to return a table of results from a PHP file into a HTML doc and update the div. 我尝试了以下代码,因为这在另一个项目中确实对我有用,将结果表从PHP文件返回到HTML文档并更新div。 Sadly this does not work either! 可悲的是,这也不起作用! - It just returns an empty div to the main page: -它将一个空的div返回到主页:

<?php

    $q = $_GET['cat'];

    include 'db_connect.php';

    $result = mysqli_query($con, "SELECT * FROM product_info WHERE ProductCat= '".$q."'");

echo "<div class='product-display'>";

while($row = mysqli_fetch_array($result)) {

        echo "<img id='prodImage' src='Images/" . $row['ProductImg'] . ".jpg' alt='product photo'>";
        echo "<h3>" . $row['ProductTitle'] . "</h3>";
        echo "<h3>" . $row['ProductDesc'] . "</h3>";

    }

   echo "</div>";

?>

Not really sure where to go from here and to be honest I have sneaky suspicion that I am approaching the whole 'product filter' idea in the least efficient way possible! 我不太确定该从何而来并说实话,我暗中怀疑我正在以最不可行的方式来接近整个“产品过滤器”的想法! Any pointers are appreciated.... 任何指针表示赞赏...。

EDIT - Oh and I have also attempted to use global variables and then echo the result outside of the while loop - but it just returned an empty result to the main product page. 编辑-哦,我也尝试过使用全局变量,然后在while循环之外回显结果-但它只是向主产品页面返回了空结果。

CORRECT ANSWER: It was something small all along! 正确答案:一直很小! This was the option code I had: 这是我的选择代码:

<div class="product-filter-bar">
        <div>
            <form class="product-filter-cat" onchange="selectCat(this.value)">
            <select>
                <option>Select a category:</option>
                <option>Top</option>
                <option>Jacket</option>
                <option>Dress</option>
                <option>Jeans</option>
                <option>Skirt</option>
            </select>
            </form>
        </div>   
    </div>

But this should have been: 但这应该是:

<div class="product-filter-bar">
        <div>
            <form>
            <select class="product-filter-cat" onchange="selectCat(this.value)">
                <option>Select a category:</option>
                <option>Top</option>
                <option>Jacket</option>
                <option>Dress</option>
                <option>Jeans</option>
                <option>Skirt</option>
            </select>
            </form>
        </div>   
    </div>

**I simply had the function call attached to the "form" tag and should have had it attached to the "select" tag. **我只是将函数调用附加到“ form”标签上,并且应该将其附加到“ select”标签上。

Try using the developer tools in you browser to do a console log on the response of the ajax request. 尝试在浏览器中使用开发人员工具对ajax请求的响应进行控制台日志。 This will allow you to see if anything is being returned and narrow down is it you PHP or your Javascript. 这将使您查看是否返回了任何内容,并缩小范围是PHP还是Javascript。

My Java script is rusty but are you sure that xmlhttp.onreadystatechange is working correctly? 我的Java脚本生锈了,但是您确定xmlhttp.onreadystatechange正常工作吗?

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

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