Solved

using the response from ajax in global var

Posted on 2013-12-03
21
216 Views
Last Modified: 2013-12-04
following the last example on the page here: example

my code looks like this and is functioning:
<script>
var new_waiverid = 0;
	$.noConflict();
	jQuery( document ).ready(function( $ ) {


		$(function () {
			$("#formDuplicate").on("submit",function(e) {
				e.preventDefault(); // do NOT submit
					$.ajax({
					type: "POST",
					cache: false,
					  url: 'addDupeWaiver.cfm',
					  async: false,
					  data: $(this).serialize(),
					  success: function(data) {
					   handelRequest(data),
					   	$("#formDuplicate").unbind('submit').submit();
					  }

				 });

			    function handelRequest(data) {
			        onComplete(data); //get correct value, works fine
			    }

			    function onComplete(new_waiverid){
			    	var x = new_waiverid;
			        alert(x);

			    }
				 //return data;
			});
		});//first func
	}) //doc ready
</script>

Open in new window


by works i mean i get the proper response from the cfc and the alert(x) displays the correct id for the new record.

Q. how can i get the value of x into a global var?
0
Comment
Question by:ecpeel
[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
  • 11
  • 10
21 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39693356
replace : var x = new_waiverid;
by : window.x = new_waiverid;
0
 

Author Comment

by:ecpeel
ID: 39693515
leakim971 Hello and thanks for your response.

adding window.x causes this error:

Uncaught SyntaxError: Unexpected token .
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39693545
Another one :
window["x"] = new_waiverid;
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!

 

Author Comment

by:ecpeel
ID: 39693645
leakim971,
it doesn't seem to like the []'s either.
same error
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39693680
could you post the whole code (copy/paste)
0
 

Author Comment

by:ecpeel
ID: 39693789
I thought I did.
The componet called from the addDupeWaiver.cfm page does indeed function correctly but if you want to test that page and mimic a result simply created it and put
<cfoutput>1234</cfoutput>

Open in new window


otherwise the jquery stuff is:

<script>
var new_waiverid = 0;
	$.noConflict();
	jQuery( document ).ready(function( $ ) {


		$(function () {
			$("#formDuplicate").on("submit",function(e) {
				e.preventDefault(); // do NOT submit
					$.ajax({
					type: "POST",
					cache: false,
					  url: 'addDupeWaiver.cfm',
					  async: false,
					  data: $(this).serialize(),
					  success: function(data) {
					   handelRequest(data),
					   	$("#formDuplicate").unbind('submit').submit();
					  }

				 });

			    function handelRequest(data) {
			        onComplete(data); //get correct value, works fine
			    }

			    function onComplete(new_waiverid){
			    	var window[x] = new_waiverid;
			        alert(window.x);

			    }
				 //return data;
			});
		});//first func
	}) //doc ready
</script>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 39693802
I said :
replace : var x = new_waiverid;
by : window.x = new_waiverid;

Not :
replace : var x = new_waiverid;
by : var window.x = new_waiverid;
0
 

Author Comment

by:ecpeel
ID: 39694648
apologies, I read it wrongly. the syntax error is gone.
rewrite of the function but the var is still local.

here's my current code:
<script>
window.exists =0;
function set_exists(x){
    window.exists = x;
};
	jQuery( document ).ready(function() {
		$("#formDuplicate").on("submit",function(e) {
			e.preventDefault(); // do NOT submit
			$.ajax({
				url: 'addDupeWaiver.cfm',
				type: "POST",
				async: false,
				data: $(this).serialize(),
				success: function(data){
					if(data){
						set_exists(data);
						
					}
					else{
						set_exists(false);
						
					}
				}
			});
			if(exists == true){
				return true;
			}
			else{
				return false;
			}
		})
	}); //doc ready
</script>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 39695262
rewrite of the function but the var is still local.

Open in new window


Line 2 you just need :
var exists = 0;
And line 4, the following is OK :
exists = x;

Anyway, the variable exists is currently global
0
 

Author Comment

by:ecpeel
ID: 39695592
exists  =0;
function set_exists(x){
    window.exists = x;
    console.log(window.exists ) // we're good here data is returned.
};

outside of this function (at the bottom of the page where I need this)  it is 0.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39695619
<script>
var exists  =0;
function set_exists(x){
    exists = x;
    console.log(exists ) // we're good TOO here data is returned.
};

Open in new window

0
 

Author Comment

by:ecpeel
ID: 39695726
yes, but aren't we still in the function set_exists and so the variable x now holds the value returned from the response?
if line seven was console.log(exists), we're back to the default zero.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39695732
could you post a link to see your page?
0
 

Author Comment

by:ecpeel
ID: 39695759
don't i wish?  here's the last change I made to capture.
For whatever reasons, that variable won't exist beyond the scope of the function call.

<script>
var exists =0;
function set_exists(x){
   exists = x;
   console.log(exists);  //ok we get the response
};
</script>
<script>console.log(exists) //reset to 0</script> 

Open in new window


thanks for sticking with me.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39695774
<script>
var exists =0;
function set_exists(x){
   exists = x;
   console.log(exists);  //ok we get the response
};
</script>
<script>
   set_exists(1234);
   console.log(exists) //set to 1234
</script>
0
 

Author Comment

by:ecpeel
ID: 39695820
yep, set to 1234 but not to the response.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39695828
0
 

Author Comment

by:ecpeel
ID: 39695859
Yes I do.
to me this suggests that the value of exists from the first set_exists(x)
needs to be the input of the second set_exists call.

or maybe you're suggesting something is wrong with the first set_exist() ?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39695999
So why did you said :  << ...but not to the response. >> ?
0
 

Author Comment

by:ecpeel
ID: 39696313
man you've been great and here's how we solved this.
created a form variable the exists variable in coldfusion and also as a hidden form field to be passed when the formDuplicate form is submitted.

and modified set_exists like this:
<script>
var exists=0;
function set_exists(x){
   document.forms["formDuplicate"]["exists"].value = x;
};
</script>

Open in new window


and that did the trick... this time.

Thanks for riding with me again
0
 

Author Closing Comment

by:ecpeel
ID: 39696315
extra kudos for introducing me to and using jsfiddle.
0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Bootstrap Form 16 41
Github api to fetch the number of collaborators in a git repo 1 30
Bootstrap question 4 32
Compute age Html 2 27
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…

733 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