• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 570
  • Last Modified:

Save a variable in Javascript

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
DrDamnit
Asked:
DrDamnit
  • 3
  • 2
  • 2
  • +2
1 Solution
 
burtonrhodesCommented:
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
 
DrDamnitAuthor Commented:
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
 
DrDamnitAuthor Commented:
The line that keeps failing is line 18. And these are defined in  not  because I am using a Dreamweaver template.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Michel PlungjanIT ExpertCommented:
Return JSON and have the ID as part of the JSON string
0
 
Gurvinder Pal SinghCommented:
why don't you save that variable in a hidden input element and then retrieve it from there in another JS file?
0
 
herman_bovensCommented:
The global actid is not visible in your CloseActivity function because the function has a parameter actid that hides the global variable.
0
 
Michel PlungjanIT ExpertCommented:
doh - well spotted
0
 
herman_bovensCommented:
DrDamnit, your problem should be solved if you rename/remove the actid parameter, see my comment with ID: 26281507.
0
 
DrDamnitAuthor Commented:
It's always the simple stuff that gets you! Thanks, and good catch.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now