duan02143 2017-03-16 00:27
浏览 32
已采纳

更改网页网址并根据$ _POST值保留该网址

I have a WordPress website with header.php called for every single page. On header.php, I have a form on which the user can select a product category. On change, the form will redirect the user to my category page, which is category.php on my server but appears as /category when browsing the site.

On header.php:

<form method="POST" action="category" id="cateogry-filter-pick">
    <select class="selectpicker" id="category-filter" name="cat">
        <?php 
        $category_list = $wpdb->get_results("SELECT DISTINCT product_category FROM $table_name;");
        foreach($category_list as $key => $row): ?>
            <option value="<?php echo str_replace(' ', '-', $row->product_category);?>"><?php echo $row->product_category;?></option>
        <?php endforeach; ?>
    </select>
</form>

on Custom-js.js

$(function(){
    $('#category-filter').on('change', function() {
        this.form.submit();
    }); 
});

On category.php, I can asign the category to a variable, and then loop to display all my results, using:

$product_category = isset($_POST['cat']) ? $_POST['cat'] : null;

On my category page, I am also using some sorting filters with AJAX / $_GET, which would change the URL to something like this:

/category/?sort=rating

When doing so, I lose the value of the category sent from any previous $_POST.

What I would ultimately want to do is to change my page URL from /category to /the-name-of-the-category/ (which is $_POST['cat']) and preserve this value when I use my sort filters. My URL would then become something like:

www.mywebsite.com/shoes/?sort=rating

What is the best approach to do so, considering that I also want users who type a URL in their browser such as www.mywebsite.com/shoes to access the same page.

  • 写回答

1条回答 默认 最新

  • drl57959 2017-03-16 00:37
    关注

    (sorry for bad english)

    When you submit a form (submit button or javascript), it will redirect to form's action (action="category") to:

    url: www.mywebsite.com/category
    

    if you want to be redirect to www.mywebsite.com/your-category you must do that in category.php:

    $product_category = isset($_POST['cat']) ? $_POST['cat'] : null;
    
    if($product_category)
    {
        $url = "www.mywebsite.com/".$product_category;
    }
    

    and if you want to preserve ?sort=rating or another $_GET vars without lost your previous category, you must append to your url in category.php:

    $url = "www.mywebsite.com/".$product_category."/?".$my_get_vars;
    

    in which $my_get_vars contains the query part of your previous url

    But if your filters (query vars) come from an ajax call, you must send your ajax request to current url: www.mywebsite.com/selected-category not to www.mywebsite.com/category.php to preserve your selected category.


    Hope this help!

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

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记