how to close a form opened in a modal with a button?

hello, I usually close all my modals with this:

echo "<script>window.parent.tb_remove();</script>";

but this time, in my modal, I have a button which is of type AJAX, so when I put this code inside that button, nothing happens.

If I were to make a php button this would work, but in this case I need the button to be AJAX as I use sc_ajax_message();

so how should I write that piece of code to work in an AJAX button? I tried almost everything I can think of and nothing works!

I’d try this.

Create a JS function in the app OnLoad event:

echo "<script>
function close_modal()
{
window.parent.tb_remove();
}
</script>
";

In the ajax button code, trigger the close_modal function via the sc_ajax_javascript macro:

sc_ajax_javascript('close_modal'); 

NOTES

The JS function could be implemented as an external library so that you have a centralized version to manage in case you need to edit it; being a one line function, I don’t know if it’s worh the effort.

SC has also other ways to create JS functions inside individual form apps: “Form Settings \ JavaScript” or “Programming \ JavaScript Methods” but usually I don’t use them so I don’t know if the above suggestion would work.

1 Like

this works perfectly!!!

Thank you so much robydago, you always have a solution for my problems!!

If maybe you are also an expert on Iframes and Scrollbars, I have a topic under “Grid/Reports” that I recently posted but no solution so far!

Thank you again

I forgot, how can I add in this AJAX button a Jquery as well? the difference is, I need to import library in the tag,
in my blank app the code is:

<head>
sc_include_lib('Jquery');
    sc_include_lib('jquery_thickbox');

    sc_include_lib('jkanban');    
</head>

    <script>

function open_details()
{
tb_show('', '../business_items/?KeepThis=true&TB_iframe=true&height=400&width=550', null);
}
</script>

I tried doing the same as you showed me with the Close modal function, by doing a big Echo with all this code, but it doesnt seem to work!

To import your own JS libraries you have to use the sc_url_library() macro
jQuery is embedded in SC and I use it without doing anything to load\enable it .
Don’t know about the other libraries and about the sc_include_lib() as I never used them.

But as for my js function suggestion, I guess that you have to import\enable any libraries in the app events\configuration and not in the AJAX button itself