?
Solved

Save a variable in Javascript

Posted on 2010-01-10
9
Medium Priority
?
565 Views
Last Modified: 2012-05-08
I have an ajax function that relays some information to a php script. The php script store the information in a database, and the response text containts the mysql_insert_id() so that we can know the record we are working with.

Question:

I have one function that saves the information via ajax. Then, the user (at a later time, say... 5 minutes later), completes the action, and clicks the update button.

How do I save the mysql_insert_id that was reported by the first ajax function so that I can update that record with ajax a second time?

In other words, one javascript function has a variable I need available for use in another javascript function later. how do I "save" that variable?
0
Comment
Question by:DrDamnit
[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
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 3

Expert Comment

by:burtonrhodes
ID: 26279357
Can't you define a "global" variable in your html page that both functions can see it?  For example, if you define a script in the <head> tag you could set the current id in a variable there.

<html>
<head>
<script>
var id_holder_variable = 0;
</script>
<head>
<body....
0
 
LVL 32

Author Comment

by:DrDamnit
ID: 26280365
Tried that. but it is not working for some reason. See code below. It is coming back as zero, always.
<script type="text/javascript">
	var actid = 0;
	
	function CloseActivity(actid) {
		var answer = confirm("Are you sure you want to close this activity?");
		if(answer != true) { return false; }
		//closes a ticket using ajax.
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		
		//setup basic url
		var url = '<?php echo DIR_WS_TICKETS.'closeactivity.php?actid='; ?>'+actid;
		
		//get the adjustment minutes, if any.
		var el = document.getElementById('timeoff')
		
		//append to url
		url=url+'&adjust='+el.value;
		
		//get the notes
		var el = document.getElementById('notepad');
		
		//append to url
		url=url+'&notes='+escape(el.value);
		alert(url);
		xmlhttp.open("GET",url,false);
		xmlhttp.send(null);
		document.getElementById('notes').style.display='none';
		document.getElementById('done').innerHTML=xmlhttp.responseText;
	}

	function SaveStart()
	{
		//creates the URL to submit the activity start, and then performs AJAX to save it. If returned successful, makes the save start div disappear. Otherwise, alerts with an error.
		var el = document.getElementById('desc');
		var url = '<?php printf("https://www.somedomain.com/powerpanel/tickets/startactivity.php?tixid=%s&desc=",$_REQUEST['tixid']) ?>' + el.value;
		StartActivity(url);
	}
	
	function StartActivity(url)
	{
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.open("GET",url,false);
		xmlhttp.send(null);
		var resp=xmlhttp.responseText;
		if(resp=="0")
		{
			alert('Activity could not be saved. Unknown Error.');
		} else {
			var el = document.getElementById('start');
			el.style.display='none';
			var ajaxDisplay = document.getElementById('ajaxdiv');
			//process resp
			var buffer = resp.split("|");
			ajaxDisplay.innerHTML = '<p>This activity was started at: ' + buffer[0] + "</p>";
			document.getElementById('notes').style.display = 'block';
			
			//save activity ID for later use.
			actid = buffer[1];
			
			alert('ActID:'+actid);
		}
	}
</script>

Open in new window

0
 
LVL 32

Author Comment

by:DrDamnit
ID: 26280406
The line that keeps failing is line 18. And these are defined in  not  because I am using a Dreamweaver template.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26281273
Return JSON and have the ID as part of the JSON string
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26281488
why don't you save that variable in a hidden input element and then retrieve it from there in another JS file?
0
 
LVL 2

Accepted Solution

by:
herman_bovens earned 2000 total points
ID: 26281507
The global actid is not visible in your CloseActivity function because the function has a parameter actid that hides the global variable.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 26281518
doh - well spotted
0
 
LVL 2

Expert Comment

by:herman_bovens
ID: 26291691
DrDamnit, your problem should be solved if you rename/remove the actid parameter, see my comment with ID: 26281507.
0
 
LVL 32

Author Closing Comment

by:DrDamnit
ID: 31675267
It's always the simple stuff that gets you! Thanks, and good catch.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

765 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