Solved

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

Posted on 2012-03-15
11
577 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

803 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