Solved

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

Posted on 2012-03-15
11
599 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
Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

626 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