ecpeel
asked on
using the response from ajax in global var
following the last example on the page here: example
my code looks like this and is functioning:
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?
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>
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?
ASKER
leakim971 Hello and thanks for your response.
adding window.x causes this error:
Uncaught SyntaxError: Unexpected token .
adding window.x causes this error:
Uncaught SyntaxError: Unexpected token .
Another one :
window["x"] = new_waiverid;
window["x"] = new_waiverid;
ASKER
leakim971,
it doesn't seem to like the []'s either.
same error
it doesn't seem to like the []'s either.
same error
could you post the whole code (copy/paste)
ASKER
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
otherwise the jquery stuff is:
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>
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>
I said :
Not :
replace : var x = new_waiverid;
by : var window.x = new_waiverid;
replace : var x = new_waiverid;
by : window.x = new_waiverid;
Not :
replace : var x = new_waiverid;
by : var window.x = new_waiverid;
ASKER
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:
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>
rewrite of the function but the var is still local.
Line 2 you just need :
var exists = 0;
And line 4, the following is OK :
exists = x;
Anyway, the variable exists is currently global
ASKER
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.
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.
<script>
var exists =0;
function set_exists(x){
exists = x;
console.log(exists ) // we're good TOO here data is returned.
};
ASKER
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.
if line seven was console.log(exists), we're back to the default zero.
could you post a link to see your page?
ASKER
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.
thanks for sticking with me.
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>
thanks for sticking with me.
<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>
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>
ASKER
yep, set to 1234 but not to the response.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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() ?
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() ?
So why did you said : << ...but not to the response. >> ?
ASKER
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:
and that did the trick... this time.
Thanks for riding with me again
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>
and that did the trick... this time.
Thanks for riding with me again
ASKER
extra kudos for introducing me to and using jsfiddle.
by : window.x = new_waiverid;