Makzino
Makzino

Reputation: 43

Mod Rewrite on Form Submission using GET Variables

Good day kind folks.. Now I know this question on clean URLs has been asked probably a dozen times, but none of them seem to be the problem I'm having. I'm using a while loop to generate different product_IDs for my form, which is submitted using the GET method. However, I have been able to string rewrite my URL on my htaccess file to be something like

localhost/source/p/variable1-variable2-variable3.html

I also used javascript to preventDefault submission to give me the desired URL format.

Now my problem is this.. WITHOUT the URL redirect, it gives me the different IDs but in the unclean format:

localhost/source/p?  agent_id=variable1&product_title=variable2&product_id=variable3

With the different variables on my while loop..

But WITH the Javascript redirect, it only gives me just the first id, and keeps displaying contents with that ID for more than 20 different cases. So I need help on how to provide the different IDs I should normally get. Here are my files:

MY HTACCESS FILE

Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteRule ^p/([0-9]+)-([0-9a-zA-Z_-]+)-([0-9]+)\.html$ product.php?agent_id=$1&product_title=$2&product_id=$3 [QSA, NC, L]

The php action file

<?php 
$SQL = Select * from product where.. 
$query = mysqli_query($con, $sql);
While ($row = mysqli_fetch_assoc($query)) { 
$product_id = $row ['product_id'];
$agent_id = $row ['agent_id'];
$product_title = $row ['product_title'];
?>
<form id="myform" method='get' action='p/'>
<input type="hidden" name="agent_id" value="<?php echo  $agent_id; ?>" />
<input type="hidden" name="product_title" value="<?php echo $product_title; ?>" />
<input type ="hidden" name="product_id" value="<?php echo $product_id; ?>" />
<button type="submit">View product</button>
</form>
<?php } ?>

My JAVASCRIPT

$(function(){
$("#myform").submit(function(e){
e.preventDefault();
var action = $(this).attr("action");
var agent_id = $(":input[name='agent_id']").val();
var product_title = $(":input[name='product_title']").val();
var product_id = $(":input[name='product_id']").val();
action = action + agent_id + "-" + product_title + "-" + product_id +    ".html"; 
location.href = action; 
});
});

So I don't know what I'm doing wrong.. Don't know how to loop through it to give the various IDs because I thought the while loop would change that.. But it isn't.

Sorry if it a Lil messy, typing through my phone.

Upvotes: 1

Views: 148

Answers (1)

jhilgeman
jhilgeman

Reputation: 1583

First, anytime you're not sure what .htaccess is doing when it comes to mod_rewrites, you should first turn up the logging level for mod_rewrite, since that can often point you in the right direction: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

LogLevel alert rewrite:trace3

Next, I'd recommend you provide a valid example of a URL that's being matched, and then use a tool like The Regex Coach to check to see if your pattern is actually matching the data properly.

My guess on the actual question here is that the product title isn't getting matched as expected (perhaps unexpected encoded characters or something), but the logging for mod_rewrite will tell you whether the regex is successful or not.

Finally, I'm not sure why you're using a form tag if you're not actually going to submit the form...

Why not just use PHP to build a button that redirects to that location?

...
$product_title = $row ['product_id'];

echo "<input type='button' value='View product' onclick='location.href=\"/p/".urlencode($agent_id)."-".urlencode($product_title)."-".urlencode($product_id)."\";'>\n";
?>

It'd be cleaner than trying to prevent the form submission and using Javascript to construct it all on the fly...

Upvotes: 1

Related Questions