Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-03-15
11
Medium Priority
?
610 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:Gurvinder Pal Singh
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:Gurvinder Pal Singh
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
Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

 
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:
Gurvinder Pal Singh earned 2000 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:Gurvinder Pal Singh
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:Gurvinder Pal Singh
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:Gurvinder Pal Singh
ID: 37731864
which URL? current one?

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

Featured Post

Basic Security of Your VPC

So, you’ve got this shiny new VPC and a fancy new application configured on your EC2 servers ready to go. This application is only accessible from your computer, which is great for security, but you need your users to be able to access it! So, what’s the easiest way to do this?

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to implement server side field validation and display customized error messages to the client.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

705 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