Jump to content

passing $_GET variable to javascript


subzerostudio

Recommended Posts

Hi

a have a page on my site that is passed the following in the URL 'products_id=n' - where n is a number

I use $_GET['products_id'] to get the id, and of course it works.

However when I try and use this in some javascript in the head of my code, it doesn't work - it always interprets $_GET['products_id'] as 28 - even if the products_id passed is 29 or 30


The javascript code can be seen in the head of my document (see the link below) (for some reason I can't post it here - whenever i try it redirects me to a 'the connection with the server has been reset' page.. not sure what that is all about??


The relevant line is:
[code]window.location.href = 'product_info.php?products_id=<?php echo $_GET['products_id']; ?>&colour=' + this.options[this.selectedIndex].value;[/code]

So basically what is happening is that even if products_id=30; that line will be interpreted as:

[code]window.location.href = 'product_info.php?products_id=28&colour={some colour value}[/code]

Why is this happening??

You can view the site at [url=http://www.scooter.co.uk]http://www.scooter.co.uk[/url].  And the product_info.php page can be seen by clicking any of the bikes (the site has a url rewrite function on it so it won't display as product_info.php).  The problem means that if you select either of the 50cc scooters, and change the colour drop down - the bike will change to 125cc (as this has product id 28!!)

Hope I explained myself ok  ::)

Mike
Link to comment
Share on other sites

Not sure about your problem. but you have a hidden field with the correct value:

<input type="hidden" name="products_id" value="30">

but the value in the javascript has value 28. how do you put the value in the hidden field? Use the same way to put the value in the javascript. or you can give the hidden field an id, an call it by document.getElementById('products_id').value.

var id = document.getElementById('products_id').value;
window.location.href = 'product_info.php?products_id=' + id + '&colour=' + this.options[this.selectedIndex].value;
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.