on the call back after the ajax call in qm150_submit $.post .... I want to call a second function called 'send_email' (which also has a callback called 'success_callback'
I am getting an error here
function () {send_email(fromName,fromEmail,toEmail,subject,message,success_callback) };
error: Uncaught SyntaxError: Unexpected token )
here is the code :
function qm150_submit($title, $name, $email, $description, $send_email) {
$.post('<?PHP print API_SUBMIT; ?>', { "title": $title, "name": $name, "email": $email, "description": $description },
function (data) { // callback function after API_SUBMIT
// Send email with a link to their collection
if ($send_email) {
// parameters for the send_email() ajax function
var subject = "subject";
var collection_id = data.collection_id; // data is json returned from the ajax above
var toEmail = $email
var message = "<?PHP print SHARE_COLLECTION;?>"+collection_id;
var fromEmail = "<?PHP print EMAIL_FROM_EMAIL; ?>";
var fromName = "<?PHP print EMAIL_FROM_NAME; ?>";
var success_callback = function (results) {
alert('send_email has returned with: '+results);
};
alert('I am now calling the send_email');
function () {send_email(fromName,fromEmail,toEmail,subject,message,success_callback) };
}
});
// missing a curly bracket ? no! note double indentation of the anonymous function (data) is a continuation of first statement
}
edit: and the code for the send_email()
function send_email(fromName,fromEmail,toEmail,subject,message,success_callback) {
alert('send_email called');
$.ajax({
type: 'post',
url: '<?PHP print API_SHARE_EMAIL;?>',
data: 'fromName=' + fromName + '&fromEmail=' + fromEmail + '&toEmail=' + toEmail + '&subject=' + subject + '&message=' + message,
dataType:'json',
success: success_callback
});
alert('send_email finished');
return true;
}
on the call back after the ajax call in qm150_submit $.post .... I want to call a second function called 'send_email' (which also has a callback called 'success_callback'
I am getting an error here
function () {send_email(fromName,fromEmail,toEmail,subject,message,success_callback) };
error: Uncaught SyntaxError: Unexpected token )
here is the code :
function qm150_submit($title, $name, $email, $description, $send_email) {
$.post('<?PHP print API_SUBMIT; ?>', { "title": $title, "name": $name, "email": $email, "description": $description },
function (data) { // callback function after API_SUBMIT
// Send email with a link to their collection
if ($send_email) {
// parameters for the send_email() ajax function
var subject = "subject";
var collection_id = data.collection_id; // data is json returned from the ajax above
var toEmail = $email
var message = "<?PHP print SHARE_COLLECTION;?>"+collection_id;
var fromEmail = "<?PHP print EMAIL_FROM_EMAIL; ?>";
var fromName = "<?PHP print EMAIL_FROM_NAME; ?>";
var success_callback = function (results) {
alert('send_email has returned with: '+results);
};
alert('I am now calling the send_email');
function () {send_email(fromName,fromEmail,toEmail,subject,message,success_callback) };
}
});
// missing a curly bracket ? no! note double indentation of the anonymous function (data) is a continuation of first statement
}
edit: and the code for the send_email()
function send_email(fromName,fromEmail,toEmail,subject,message,success_callback) {
alert('send_email called');
$.ajax({
type: 'post',
url: '<?PHP print API_SHARE_EMAIL;?>',
data: 'fromName=' + fromName + '&fromEmail=' + fromEmail + '&toEmail=' + toEmail + '&subject=' + subject + '&message=' + message,
dataType:'json',
success: success_callback
});
alert('send_email finished');
return true;
}
Share
Improve this question
edited Feb 12, 2012 at 9:10
johowie
asked Feb 12, 2012 at 8:13
johowiejohowie
2,4956 gold badges27 silver badges42 bronze badges
2 Answers
Reset to default 1The unexpected token is the (
after function
.
First of all, you're declaring an anonymous function without ever calling it. Secondly, an anonymous function declaration cannot be a statement (or in other words, a function statement must have a name), which is why the (
is unexpected (javascript expects a function name, not parantheses).
Simply call send_email
directly... It's already inside a function, so it won't "pollute" the global object (there's nothing to pollute it with anyway) - I see no need for an anonymous function:
alert('I am now calling the send_email for real!');
send_email(fromName, fromEmail, toEmail, subject, message, success_callback);
If you check your code with JSLint you will get this error:
Function statements cannot be placed in blocks. Use a function expression or move the statement to the top of the outer function.
You have to wrap the anonymous function like this: (function{})();
Fixed code:
function qm150_submit($title, $name, $email, $description, $send_email) {
$.post('<?PHP print API_SUBMIT; ?>', {
"title": $title,
"name": $name,
"email": $email,
"description": $description
}, function(data) { // callback function after API_SUBMIT
// Send email with a link to their collection
if ($send_email) {
// parameters for the send_email() ajax function
var subject = "subject";
var collection_id = data.collection_id; // data is json returned from the ajax above
var toEmail = $email;
var message = "<?PHP print SHARE_COLLECTION;?>" + collection_id;
var fromEmail = "<?PHP print EMAIL_FROM_EMAIL; ?>";
var fromName = "<?PHP print EMAIL_FROM_NAME; ?>";
var success_callback = function(results) {
alert('send_email has returned with: ' + results);
};
alert('I am now calling the send_email');
(function() {
send_email(fromName, fromEmail, toEmail, subject, message, success_callback);
})();
}
});
}
Or simply remove that anonymous function that does nothing:
send_email(fromName, fromEmail, toEmail, subject, message, success_callback);
instead of:
(function() {send_email(fromName, fromEmail, toEmail, subject, message, success_callback);
})();