Note that there are some explanatory texts on larger screens.

plurals
  1. POphp include and jquery .load()
    text
    copied!<p>I have a php page that shows a bunch of products and down the left hand side is a set of filters (e.g. colour, size, style). I originally had it so that when you choose a filter (e.g. green) it would post the form using the GET method and refresh the page with the filtered results. I'm now changing this so that it will refresh only the products content and not the whole page itself and I'm doing this with jquery.</p> <p>When the user visits the page with no filters selected, it uses a php include to show the initial results:</p> <pre><code>&lt;div id="products"&gt; &lt;?php include('products.php') ?&gt; &lt;/div&gt; </code></pre> <p>When a user clicks a filter, I'm using jquery .load() to load the the same include in its place but with the filters in the querystring:</p> <pre><code>$("#products").load("products.php?colour=green"); </code></pre> <p>When it refreshes the products div, it does the php bit fine and only shows green products.</p> <p>However, it still runs the php include under the filtered results which means I have the filtered results showing and then the php include results showing underneath and I'm not sure why!</p> <p>I have to have the php include first so that if a search engine indexes by page, it will see there's content as search engines won't execute JavaScript (see answer to <a href="https://stackoverflow.com/questions/14688490/search-engine-visibility-and-jquery-load-function">Search engine visibility and jquery load function</a>).</p> <p>Can anyone help me with this?</p> <p>EDIT: Here's my code.</p> <p>shop.php</p> <pre><code>&lt;form id="filterproducts" action="" method="get"&gt; &lt;div id="filters"&gt; &lt;p&gt;Colour&lt;/p&gt; &lt;input type="checkbox" name="colour" value="red" /&gt; Red &lt;input type="checkbox" name="colour" value="blue" /&gt; Blue &lt;input type="checkbox" name="colour" value="green" /&gt; Green &lt;input type="checkbox" name="colour" value="yellow" /&gt; Yellow &lt;input type="hidden" name="colours" value="" /&gt; &lt;/div&gt; &lt;/form&gt; &lt;div id="products"&gt; &lt;?php include("products.php"); ?&gt; &lt;/div&gt; &lt;script&gt; $(':checkbox').change(function() { var colour = $(this).val(); $("#products").load("products.php?colour="+colour); }); &lt;/script&gt; </code></pre> <p>products.php</p> <pre><code>&lt;?php $sql = "SELECT id, productname, productprice FROM products"; if (isset($_GET['colour']) &amp;&amp; $_GET['colour'] != "") { $colour = $_GET['colour']; $sql .= " WHERE colour = '$colour'"; } $sql .= " ORDER BY productprice LIMIT 10"; $result = mysql_query($sql,$link); if (mysql_num_row($result) &gt; 0) { while($line = mysql_fetch_assoc($result) { extract($line); ?&gt; &lt;div id="product-&lt;?php print $id ?&gt;"&gt; &lt;p&gt; &lt;a href="http://www.myshopsite.com/product/product-&lt;?php print $id ?&gt;" title="&lt;?php print $productname ?&gt;"&gt; &lt;img src="/images/product-&lt;?php print $id ?&gt;" alt="&lt;?php print $productname ?&gt;" /&gt; &lt;br /&gt; &lt;?php print $productname ?&gt; &lt;br /&gt; &lt;?php print $productprice ?&gt; &lt;/a&gt; &lt;/p&gt; &lt;/div&gt; &lt;?php } } else { ?&gt; &lt;p&gt;Sorry, there are no products to show&lt;/p&gt; &lt;?php } ?&gt; </code></pre> <p>I've only added the very basic code, just so you can see what I'm doing.</p> <p>NEW EDIT: I've tested the .load() bit by putting some normal text in #products and then running the click function:</p> <pre><code>&lt;div id="products"&gt; &lt;p&gt;This is some test text&lt;/p&gt; &lt;/div&gt; </code></pre> <p>When I click a filter, .load() replaces the <p> with whatever is in .load() so I know this bit works, it's just not removing the php include content...</p>
 

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