dongyuxin5353 2016-11-26 08:00
浏览 36
已采纳

Echo php数组结果外循环 - 使用jquery更新html div

Ok so this one has been racking my brain. I have the following page 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:

<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.

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.

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. Sadly this does not work either! - It just returns an empty div to the main page:

<?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.

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.

  • 写回答

1条回答 默认 最新

  • doulin1867 2016-11-26 08:08
    关注

    Try using the developer tools in you browser to do a console log on the response of the ajax request. This will allow you to see if anything is being returned and narrow down is it you PHP or your Javascript.

    My Java script is rusty but are you sure that xmlhttp.onreadystatechange is working correctly?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 php 将rtmp协议转hls协议,无法播放
  • ¥15 miniconda安装不了
  • ¥20 python代码编写
  • ¥20 使用MPI广播数据遇到阻塞
  • ¥15 TinyMCE如何去掉自动弹出的“链接…”工具?
  • ¥15 微信支付转账凭证,如何解决
  • ¥15 在win10下使用指纹登录时,界面上的文字最后一个字产生换行现象
  • ¥20 使用AT89C51微控制器和MAX7219驱动器来实现0到99秒的秒表计数,有开始和暂停以及复位功能,下面有仿真图,请根据仿真图来设计c语言程序
  • ¥15 51单片机 双路ad同步采样
  • ¥15 使用xdocreport 生成word