How to add custom javascript just before block data is saved
Permalink
Greetings mercenaries, I am Silke - Thespian, extraordinaire. But apart from that, I was wondering how one could add some custom javascript just before block data is being saved. That is, I have a custom block type. When add a new block of this type, the add.php gets called. When I click the Add button, the data is being passed for saving. I need some javascript to be executed when the Add button is clicked, but before the data is being sent for processing. I have several input fields and I want to store all their information in a coded form in one field. Javascript should be putting the correct data in the correct field, before the whole thing gets sent, so that you know what I am trying to do. Thanks in advance!
I'm just thinking out loud here, but can't you override default behaviour of the submit button with js/jQuery, do your stuff and then send the post data?
As far as I understand the code, there is no submit button. It is a link which points to some page, which does some redirection but I can not find where is the code that is executed. :/
If it is simple validation you want to do, you can define a function in a script block in your add/edit dialog:
These function names are known to C5, so that is all you need.
For more complex interactions, I have found that catching the click event of the 'Save' button (link) works well as that can return true or false to allow the click action to continue. It also works for the Cancel button.
<script type="text/javascript"> function ccmValidateBlockForm() { if ($.trim($('input[type="text"][name="heading"]').val()).length<1){ ccm_addError('<?php echo t("You must enter a heading.");?>'); return false; } return true; } </script>
These function names are known to C5, so that is all you need.
For more complex interactions, I have found that catching the click event of the 'Save' button (link) works well as that can return true or false to allow the click action to continue. It also works for the Cancel button.
Thanks! Validation is not my aim, but the catching of the click event was the key to what I was trying to do.