How to set variable in javascript from url and POST variable?

Hi Experts,

1. How to set variable in javascript from url?
2. How to set variable in javascript from POST variable?

For example:
I have my url: and POST cache_id = 180

I want to set my variable myproductid to 87 and mycacheid to 180.

<script language="javascript">
	var myproductid = 0;
	var mycacheid = 0;
	function savedata()
		var query = $.ajax({
			type: "POST",
			cache: false,
			url: "<?php echo site_url('/product/additem'); ?>",
			dataType: 'json',
			data: { productid: myproductid, cacheid: mycacheid},
			success: function(theresponse) {
				alert("ID: " + myproductid + "CACHE: " + myproductid);							
			error: function() {
				$("#response").html("AJAX request failed.");

Open in new window

Any suggestions?

Thanks before.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gurvinder Pal SinghCommented:
are these variable values same that you are setting in your Ajax call, or you are receiving them as Ajax response?
jimmycdinataAuthor Commented:
Both variables come from a different page. I want to initiate the variable in javascript before i make ajax call.
Gurvinder Pal SinghCommented:
what do you mean by 'different page'?
if you are able to use the value in Ajax call, there is no reason that you can't put that in a javascript variable.

Just tell me from where you are getting these values and in what format (which line of code?).
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

jimmycdinataAuthor Commented:
My 'different page' is a product list: I want to edit a product with id = 87, so when I clicked the link, it pointed me to

I want to make sure that I can retrieve the productid (87) from url before I make update by ajax call to my product's table.
Gurvinder Pal SinghCommented:
var url = "";

var productId = url.substring(url.lastIndexOf("/")+1);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jimmycdinataAuthor Commented:

var url = "";

var productId = url.substring(url.lastIndexOf("/")+1);
My url is not in variable. How to get url value using javascript?
Gurvinder Pal SinghCommented:
just the way you are getting it now

var url = "<?php echo site_url('/product/additem'); ?>";
jimmycdinataAuthor Commented:
var url = "<?php echo site_url('/product/additem'); ?>";
I can't set like this way, because my page haven't know yet what productid will be set.
Gurvinder Pal SinghCommented:
your page will know by the time it is loaded into the browser.

Otherwise you need to tell me that from where i will get the information
jimmycdinataAuthor Commented:
Any javascript code to retrieve url instead php?
Gurvinder Pal SinghCommented:
which URL? current one?

You can get current url using
var url = location.href;
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.