Skip to content

Instantly share code, notes, and snippets.

@patpohler
Created March 21, 2012 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patpohler/2147754 to your computer and use it in GitHub Desktop.
Save patpohler/2147754 to your computer and use it in GitHub Desktop.
Use ajaxSubmit to post to more than one channel in ExpressionEngine (EECMS) with Safecracker
<!-- first form -->
{exp:safecracker id="form1" json="yes" channel="channel1" include_jquery="no"
return="safecracker/ENTRY_ID"
safecracker_head="no"}
Title: <input type="text" name="title" value="{title}" />
{/exp:safecracker}
<!-- second form -->
{exp:safecracker id="form2" json="yes" channel="channel2" include_jquery="no"
return="safecracker/ENTRY_ID"
safecracker_head="no"}
Title: <input type="text" name="title" value="{title}" />
{/exp:safecracker}
<!-- our submit "button", note the "onclick" if I used jQuery to wire the event, it would submit twice! weird...if someone can get this to work without "onclick" let me know! -->
<a href="#" onclick="return submitForms();" id="submitforms">Save Changes</a>
<script type="text/javascript">
function submitForms() {
//disable button to prevent double submits
$('a#submitforms').attr("disabled", "disabled");
/* first submit */
postAjaxSafeCracker($('#form1'), function(data) {
if(data.success == 1) {
/* second submit, data.entry_id can get you the entry_id of the first channel entry you save */
postAjaxSafeCracker($('#form2'), function(data) {
if(data.success == 1) {
/* redirect or do something else */
alert('both submitted!');
}
});
}
$('a#submitforms').removeAttr("disabled");
});
return false;
}
/* utility function to easily submit the form, requires http://jquery.malsup.com/form/ */
function postAjaxSafeCracker(elem, success) {
$(elem).ajaxSubmit({
type: "POST",
dataType: "json",
iframe: true,
success: success,
error: function(xmlRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
</script>
@patpohler
Copy link
Author

BTW, make sure you include jQuery in your header (or you can use safecracker's...personally I prefer to use my own)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment