在脚本中使用变量而不是设置变量的位置(ajax)

问题描述:

I got an ajax script which sorts products by price from lowest to high. This worked when I didn't have dynamic pages, but now data is returned according to the page alias (urlname).

So the following query:

$product = "SELECT * FROM `snm_content`
            WHERE `catid` = 15 AND state = 1 order by introtext ASC";

Needs to be:

$produc = "SELECT * FROM `snm_content` 
           WHERE `catid` = '".$conn->real_escape_string($productcatcr[0]['id'])."' 
           AND state = 1 order by introtext ASC";

But the problem is, the other file doesn't recognize $conn->real_escape_string($productcatcr[0]['id']) because it is used on another page.

Do I need to post it and then get it or something?

Here the correct script is loaded depending what option is selected:

<div class="form-group">
    <label class="grey" for="orderby">Sorteer op:</label>
    <select class="form-control orderby" name="orderby" id="orderby">
        <option value="default" data-post-url="default.php">Standaard</option>
        <option value="popularity" data-post-url="populairst.php">Meest bekeken</option>
        <option value="highlow" data-post-url="prijshooglaag.php">Prijs: Hoog naar laag</option>
        <option value="lowhigh" data-post-url="prijslaaghoog.php">Prijs: Laag naar hoog</option>
    </select>
</div>

Ajax:

$("#orderby").on('change', function() {

    var option = $('#orderby > option').filter(':selected');

    $.post("ajax/" + option.data("post-url"), { 
        filter: option.val() 
    }, function(result){
        $("#productviewajax").html(result);
    });

});

prijslaaghoog.php is where $productcatcr[0]['id'] needs to be recognized.

I fixed it by posting the variable through the url like so:

$pid = $productcatcr[0]['id'];

<option value="lowhigh" data-post-url="prijslaaghoog.php?id='.$pid.'">Prijs: Laag naar hoog</option>

And then in my query:

$product = "SELECT * FROM `web_content` WHERE `catid` = '".$_GET['id']."' AND state = 1 order by introtext ASC";