简体   繁体   中英

Unusual behaviour of Htaccess URL-Rewrite

I'm using jQuery Autocomplete (fetching values from a database) and displaying results to the user from the database. The search results are clickable and when they click, they are sent to a page called product_display.php with id as the request parameter. Therefore example, it would be something like this :- product_display.php?id=2

On the product_display.php page, there are 3 radio buttons and 3 forms (one for each radio button, the forms open dynamically upon radio select). Each form has a separate submit button. Upon hitting submit, it goes to a different page and the value of a hidden variable named cond is printed.

Everything was working fine until I introduced a Url-Rewrite in my htaccess file, to conver *product_display.php?id=2* to /products/2

When this url-rewrite came into work, the results were not good. As soon as I click the submit button for any form pertaining to any radio button, it again displays the options which were on the product_display page (but header shows different page). How can this be fixed.

jQuery Auto Complete.

<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 (the page which opens upon clicking submit)

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



?>

It's probably a relative/absolute URL thing. Your form has this as its action:

action="offer_show.php"

which means when you go to: /products/123 , the forum gets submitted to /products/offer_show.php , which gets rewritten to /product_display.php?id=offer_show.php .

Try changing the form's action to an absolute URL:

action="/offer_show.php"    

And you may also want to add

<base href="/" />

to your page's header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM