Avatar of jimmycdinata
jimmycdinata
Flag for Indonesia asked on

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: www.something.com/product/87 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.");
			}
		});
	}
</script>

Open in new window


Any suggestions?

Thanks before.
JavaScriptWeb Languages and StandardsScripting Languages

Avatar of undefined
Last Comment
Gurvinder Pal Singh

8/22/2022 - Mon
Gurvinder Pal Singh

are these variable values same that you are setting in your Ajax call, or you are receiving them as Ajax response?
jimmycdinata

ASKER
Both variables come from a different page. I want to initiate the variable in javascript before i make ajax call.
Gurvinder Pal Singh

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?).
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
jimmycdinata

ASKER
My 'different page' is a product list: www.something.com/product. I want to edit a product with id = 87, so when I clicked the link, it pointed me to  www.something.com/product/87.

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.
ASKER CERTIFIED SOLUTION
Gurvinder Pal Singh

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
jimmycdinata

ASKER

var url = "www.something.com/product/87";

var productId = url.substring(url.lastIndexOf("/")+1);
My url is not in variable. How to get url value using javascript?
Gurvinder Pal Singh

just the way you are getting it now

var url = "<?php echo site_url('/product/additem'); ?>";
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
jimmycdinata

ASKER
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 Singh

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
jimmycdinata

ASKER
Any javascript code to retrieve url instead php?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Gurvinder Pal Singh

which URL? current one?

You can get current url using
var url = location.href;