dtstnjl898781429 2016-10-20 15:58
浏览 49
已采纳

如何使用PHP将查询字符串修改为普通URL?

I have following html form :

<div class="row form">
    <div class="col-md-offset-3 col-md-6 col-md-offset-3">
    <form class="form-inline" role="form" method="get" action="<?php echo htmlspecialchars(SITE_URL.'location'); ?>">
        <div class="form-group col-md-10 no-p-m">                      
            <select class="form-control" id="basic" name="location" required>
            <?php
            $get_location = mysqli_query($conn, "SELECT * FROM product_sub_area");
            if(mysqli_num_rows($get_location) == 0 ) {
                $choose = 'No City found';
            } else {
                $choose = 'Choose City';
            }
            ?>
            <option value=""><?php echo $choose; ?></option>
            <?php                                         
            while($get_location_result = mysqli_fetch_array($get_location) ) {
                $location_id = (int) $get_location_result['psub_area_id'];
                $location_name = htmlspecialchars($get_location_result['psub_name']);
                echo "<option value='$location_id'>$location_name</option>";
            }
            ?>                    
            </select>
        </div>
        <div class="form-group col-md-2 no-p-m" id="basic">  
            <input type="submit" class="btn btn-default filter-search" value="BROWSE">
        </div> 
    </form>                
    </div>
</div>  

When I submit this form the url is showing like bellow :

http://localhost/freelancer/happiechef/location?location=1

But I need to show this type of url :

http://localhost/freelancer/happiechef/location/carlifonia

here /location/ is a page called location.php. I am hiding page extension using .htaccess.

So from location.php page I want to get $_GET['location'] value = $location_id

for eg.

$_GET['location'] = 1 or 2 ( select tag option value)

Existing .htaccess code

ErrorDocument 404 not-found.php
RewriteEngine On        

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^location/([\w-]+)$ location.php?location=$1 [QSA,NC,L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
  • 写回答

1条回答 默认 最新

  • dongyue6199 2016-10-20 16:37
    关注

    Here is an example of how this can be accomplished in jQuery:

    First, add an id to your <form> tag so you can access it easily:

    <form id="formname" class="form-inline" role="form" method="get" action="<?php echo htmlspecialchars(SITE_URL.'location'); ?>">
    

    You also have a duplicate id called "basic". Make sure you leave it only on the <select> tag and remove the other one.

    Then in your JS:

    $("#formname").submit(function(event) {
        event.preventDefault();
        var loc = $("#basic option:selected").text();
        window.location.href = '/freelancer/happiechef/location/' + loc;
    });
    

    This will forward the user to the URL, for example http://localhost/freelancer/happiechef/location/carlifonia. Then .htaccess will rewrite this URL to location.php?location=california.

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

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!