drxnfdx798517235 2016-08-15 13:55
浏览 46
已采纳

在脚本中使用变量而不是设置变量的位置(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.

  • 写回答

1条回答 默认 最新

  • duanjiwei1283 2016-08-15 14:24
    关注

    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";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题