Htaccess URL-Rewrite的异常行为

时间:2014-02-15 19:18:56

标签: javascript php jquery html .htaccess

我正在使用jQuery Autocomplete(从数据库中获取值)并从数据库向用户显示结果。搜索结果是可点击的,当他们点击时,他们会被发送到名为product_display.php的页面,其中id为请求参数。 例如,它会是这样的: - product_display.php?id = 2

在product_display.php页面上,有 3个单选按钮和3个表单(每个单选按钮一个,表单在无线电选择时动态打开)。每个表单都有一个单独的提交按钮。点击提交后,它会转到另一个页面,并打印一个名为cond的隐藏变量的值。

在我的htaccess文件中引入了Url-Rewrite之前,一切都运行正常,以转换为* product_display.php?id = 2 *到 / products / 2

当这个url-rewrite开始工作时,结果并不好。只要我点击任何与任何单选按钮相关的表单的提交按钮,它就会再次显示product_display页面上的选项(但标题显示不同的页面)。 如何解决这个问题。

jQuery自动完成。

<script>
  $(function() {
    function log( message ) {
      $( "<div>" ).text( message ).prependTo( "#log" );
      $( "#log" ).scrollTop( 0 );
    }

    $( "#birds" ).autocomplete({
      source: "search.php",
      minLength: 2,
      select: function( event, ui ) {


        log( ui.item ? "Selected: " + ui.item.value + " aka " + ui.item.label :
          "Nothing selected, input was " + this.actor );
         window.location.href = './products/' + ui.item.value;
         //window.location.href = 'product_display.php?id=' + ui.item.value;
       // document.testForm.action = "pretravel.php?id="+ui.item.value;
        //document.testForm.submit();
      }
    });
  });

products_display.php

<div id="credit-card">
        <section id="content">


     <input type="radio" id="radio1" name="radios" value="radio1" checked>
   <label for="radio1">Working</label>

<input type="radio" id="radio2" name="radios" value="radio2">
   <label for="radio2">Working - Damaged</label>

    <input type="radio" id="radio3" name="radios" value="radio3">
   <label for="radio3">Not Working</label>

            <form action="offer_show.php" method="post" id="working">
<input type="hidden" name="cond" value="working" id="cond">
<input type="submit" name="submit">
            </form>

    <form action="offer_show.php" method="post" id="workingdamaged">        
DEbit Card payment here
<input type="hidden" name="cond" value="workingdamaged" id="cond">
<input type="submit" name="submit">
    </form>



    <form action="offer_show.php" method="post" id="nonworking">
                <input type="hidden" name="cond" value="damaged" id="cond">
                      <input type="submit" name="submit">
            </form>




        </section>

    </div>


</body>

<script type="text/javascript">
var radios = document.getElementsByName("radios");
var working =  document.getElementById("working");
var workingdamaged =  document.getElementById("workingdamaged");
var nonworking =  document.getElementById("nonworking");
working.style.display = 'block';   // show
workingdamaged.style.display = 'none';
nonworking.style.display = 'none';// hide
for(var i = 0; i < radios.length; i++) {
    radios[i].onclick = function() {
        var val = this.value;
        if(val == 'radio1')
        {
            working.style.display = 'block';
            workingdamaged.style.display = 'none';
            nonworking.style.display = 'none';
        }
        else if(val == 'radio2')
        {
            working.style.display = 'none';
            workingdamaged.style.display = 'block';
            nonworking.style.display = 'none';
        }
        else if(val == 'radio3')
        {
            working.style.display = 'none';
            workingdamaged.style.display = 'none';
            nonworking.style.display = 'block';
        }    

    }
}
</script>

htaccess的

RewriteRule ^products/(.+)$ product_display.php?id=$1 [L,QSA]

offer_show.php(点击提交时打开的页面)

<?php
echo $_REQUEST['cond'];



?>

1 个答案:

答案 0 :(得分:2)

这可能是相对/绝对URL的事情。您的表单将此作为其操作:

action="offer_show.php"

这意味着当您转到:/products/123时,论坛会提交至/products/offer_show.php,并会被重写为/product_display.php?id=offer_show.php

尝试将表单的操作更改为绝对URL:

action="/offer_show.php"    

您可能还想添加

<base href="/" />

到您网页的标题。