Solved

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

Posted on 2012-03-15
11
565 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
This article will show, step by step, how to integrate R code into a R Sweave document
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now