Closing a Floatbox

Page: 1

Author Post
Member
Registered: Jan 2012
Posts: 98
I know (or I think I know) this has something to do with fb.end() but I can't quite get it.

First I have a blank page with an external page in an iframe and a floatbox automatically opening over it.
<a href="htmlForm.php" class="floatbox region"data-fb-options="autoStart:true minBoxWidth: 450, width:350 scrolling:no"></a>
<iframe src="pageFrom EternalDomain.html" width="100%" height="4000px" marginheight="0" frameborder="0"></iframe>


When the submit button in htmlForm.php is clicked, it links to emailPage.php that sends a couple of emails using mail/mime, but has no visible content.
<form name="refer" action="emailPage.php" method="post">
<input name="email" type="email" size="40" maxlength="50">
<input name="hid" type="hidden" value="<?php echo $hid; ?>">
<input name="thisBook" type="hidden" value="<?php echo $thisBook; ?>">
<input name="submit" type="submit" value="Go">


Once the php code in emailPage.php is complete, I want the floatbox to close, so that it leaves the external page in the iframe in the actve window.

I feel I'm nearly there, but nothing I have tried actually works.
Administrator
Registered: Aug 2008
Posts: 3382
When you submit a form, the browser will fetch and display the action page just as if a link to that page was clicked. So if you do a standard submit to an emailer page that returns blank content, that blank content will be displayed in response to the submit.

A quick and clean way to get the floatbox to close on form submit is to have the action page (the emailer) return a small script that will fire fb.end(). If the action page returns nothing but
<script>
parent.fb.end();
</script
then the form in the floatbox will be briefly replaced by the white page and then the floatbox will animate out.

A versatile and robust way to process forms displayed in a floatbox is to not do submits at all, but use AJAX to send the form data and process the response. Here's a shell of this approach to give the idea:
<form id="theForm">
<input type="hidden" name="foo" value="bar" />
<button onclick="send( "theForm" )">Submit</button>
</form>

<script>
if (!window.fb) {
window.fb = parent.fb;
}

function send ( theFormId ) {
// optional client side form validation goes here
fb.ajax({
source: "action.php",
postData: theFormId,
finish: function ( xhr ) {
if ( xhr.responseText == "ok" ) {
fb.end();
} else {
// do something else
}
}
});
}
</script>

This example assumes that action.php will return just the short string "ok" if everything's fine. It can do some server side validation and, if something's not right, return some other text which your form page can respond to in the "finish" function as it sees fit.

The postData parameter invokes a POST request. If you want to do a GET instead, remove the postData entry and change source to "action.php?" + fb.serialize(theFormId),

This AJAX form submission can be adapted to pretty much any situation as it provides opportunity for pre-submission client-side processing, server-side processing, and post-response client-side processing. It has no visual affect on the displayed form other than that explicitly invoked in your response code, and it does not have to trouble itself with handling the serial page navigation that occurs from a standard form submit.
Member
Registered: Jan 2012
Posts: 98
Superb! Now working exactly as I wanted using the little parent.fb.end() script. :D

I'll look into the ajax thing later.

Page: 1