views:

612

answers:

5

Hi!

I'm loading an inline registration form in a FancyBox from jQuery. However after submitting the form, the box immediately closes while there is some feedback that I want to show the user in the FancyBox itself. This feedback is generated on the server side and is printed in the FancyBox.

How can I make the box only closing when their is no feedback anymore? I was thinking about using ajax to just refresh the FancyBox itself and not the whole page after refreshing. But I just can't figure out how this ajax $.ajax({type, cache, url, data, success}); works... Also it seems like there's no reaction from the 'submit bind' in the javascript.

I hope someone can help me with this problem. I paste my code below. If any questions, plz ask..

Thx in advance!

This is the javascript:

<script type="text/javascript">
        $(document).ready(function() {
            $("#various1").fancybox({
                'transitionIn'      : 'none',
                'transitionOut'     : 'none',
                'scrolling'         : 'no',
                'titleShow'         : false,
                'onClosed'          : function() {
                    $("#registration_error").hide();
                }
            });
        });

        $("#registration_form").bind("submit", function() {


            if ($("#registration_error").val() != "Registration succeeded!") {
                $("#registration_error").show();
                $.fancybox.resize();
                return false;
            }

            $.fancybox.showActivity();

            $.ajax({
                type        : "POST",
                cache       : false,
                url         : "/data/login.php",
                data        : $(this).serializeArray(),
                success     : function(data) {
                    $.fancybox(data);
                }
            });

            return false;
        });

This is the inline form that I show in the FancyBox:

<div style="display: none;">
        <div id="registration" style="width:227px;height:250px;overflow:auto;padding:7px;">
        <?php echo "<p id=\"registration_error\">".$feed."</p>"; ?>
        <form id="registration_form" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
            <p>
                <label for="username">Username: </label>
                <input type="text" id="login_name" name="username" size="30" />
            </p>
            <p>
                <label for="password">Password: </label>
                <input type="password" id="pass" name="pw" size="30" />
            </p>
            <p>
                <label for="repeat_password">Repeat password: </label>
                <input type="password" id="rep_pass" name="rep_pw" size="30" />
            </p>
            <p>
                <input type="submit" value="Register" name="register" id="reg" />
            </p>
            <p>
                <em></em>
            </p>
        </form>
        </div>
    </div>
A: 

Nevermind, solved the problem..

Dimitri
would be nice if you posted the solution for other people to benefit from.
Steve Kemp
A: 

Hi, I have a similar problem, how did you solve it? Thnx

José Ignacio
+1  A: 

Hi,

I used the ajax part of the fancybox..

$.ajax({
                    type        : "POST",
                    cache       : false,
                    url         : "data/register.php",
                    data        : $(this).serializeArray(),
                    success     : function(data) {
                        data = $.parseJSON(data);

                    if (data.feedback == "Registration succeeded!") {
                      $.fancybox("Welcome, "+data.name+"! You can now log in.");
                      $.fancybox.resize();
                    }
                    else{
                      $("#registration_error").html(data.feedback);
                    }
                }
            });

In the register.php file, I checked if the data was correct or not and made some feedback. I putted the feedback in register.php in a JSON object that I could use in the 'success' option of the ajax in my main page (see above). If the feedback was positive, I made the fancybox, if not I putted the feedback in a div #registration_error in my registration form.

The code of my register.php file looks like this:

$feed = "";

// Put post vars in php vars
$username = $_POST[username];
$password = $_POST[pw];
$rep_password = $_POST[rep_pw];

// Check if php vars are not empty and check if the username doesn't exist in my user.class
if(!empty($username) && !empty($password) && !empty($rep_password))
{
    $user = new User();

    $user->Username = $username;
    $user->Password = $password;
    $user->Password2 = $rep_password;

    try
    {
        $user->Save();
        $feed .= "Registration succeeded!";
    }
    catch(Exception $e)
    {
        $feed = $e->getMessage();
    }   
}
else
{
    $feed = "Please fill in all the fields!";
}

// Feedback for fancybox
$output = array();

$output['name'] = $username;
$output['feedback'] = $feed;

// Send JSON
$output = json_encode($output);
print $output;

Hope this will help you!

GRtz!

Dimitri
A: 

I did it this aways.

I basically triggered a link that opens a form if $_POST['xxx'] is set.

First jquery function opens a form if an element id=form is clicked. Second function will triger element id=triger_form that will open the same form.

$(document).ready(function() {

$("a#form").fancybox({
    'hideOnContentClick': false
});
$("a#triger_form").fancybox().trigger('click')({
    'hideOnContentClick': false
});

});

if($_POST['refer_submit'] == TRUE) {

echo ''; }

marcin_koss
A: 

for an iframe opened fancybox, here is the code printed after the submition:

<script type='text/javascript'>
  $(document).ready(function(){
    parent.document.location.reload();
    parent.jQuery.fancybox.close();
  });
</script>
Olivier