javascript - Htaccess URL 重写的异常行为

标签 javascript php jquery html .htaccess

我正在使用 jQuery 自动完成(从数据库中获取值)并向用户显示数据库中的结果。搜索结果是可点击的,当点击时,它们会被发送到一个名为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'];



?>

最佳答案

这可能是一个相对/绝对 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="/" />

到页面的标题。

关于javascript - Htaccess URL 重写的异常行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21802375/

相关文章:

c# - 调用javascript函数从后面的代码打印表单

javascript - Vue - 检查用户是否离线,然后在他们恢复在线后显示一秒钟的 div

PHP 简单 CSS 字符串解析器

javascript - 使 jQuery 分页/排序适用于同一页面上的多个表

ajax - 多个异步 jQuery AJAX POST 同时返回

javascript - 自动访问 json 中的字母数字索引

javascript - 字符串上的正则表达式多重匹配

php - 由于 php 超时,50 个或更多文件上传未完成

php - 我对 mysqli_insert_id 有点困惑

javascript - 更改查询字符串而不重新加载/刷新