Solved

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

Posted on 2012-03-15
11
590 Views
Last Modified: 2012-03-18
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.
0
Comment
Question by:jimmycdinata
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37723765
are these variable values same that you are setting in your Ajax call, or you are receiving them as Ajax response?
0
 
LVL 2

Author Comment

by:jimmycdinata
ID: 37723779
Both variables come from a different page. I want to initiate the variable in javascript before i make ajax call.
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37723787
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?).
0
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
LVL 2

Author Comment

by:jimmycdinata
ID: 37723804
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.
0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 37723855
var url = "www.something.com/product/87";

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

Author Comment

by:jimmycdinata
ID: 37723865

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?
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37723888
just the way you are getting it now

var url = "<?php echo site_url('/product/additem'); ?>";
0
 
LVL 2

Author Comment

by:jimmycdinata
ID: 37724172
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.
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37724195
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
0
 
LVL 2

Author Comment

by:jimmycdinata
ID: 37727370
Any javascript code to retrieve url instead php?
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37731864
which URL? current one?

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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question