Page 1 of 1

Question about a redirection with jQuery

Posted: Sun Jun 27, 2010 8:46 am
by ericblanc
Hi, here's my problem, i've built a login form which is using jquery validation. The jQuery function calls a php file (with ajax), the php file check if the member exists and it returns an echo if the login is successfull or not. The problem is that now I don't want to make an echo but more like a Header: Location (redirection). My page doesn't load complety, in fact, only the bottom part is redirecting.

loginform.html

Code: Select all

<script type="text/javascript" charset="utf-8">
    jQuery(function() {
        // show a simple loading indicator
        var loader = jQuery('<div id="loader"><img src="images/loading.gif" alt="loading..." /></div>')
            .css({position: "relative", top: "1em", left: "25em"})
            .appendTo("body")
            .hide();
        jQuery().ajaxStart(function() {
            loader.show();
        }).ajaxStop(function() {
            loader.hide();
        }).ajaxError(function(a, b, e) {
            throw e;
        });
        
        jQuery("#form").validate({
            submitHandler: function(form) {
                jQuery(form).ajaxSubmit({
                    target: "#result"
                });
            },
            
            rules: {
            user: {
                required: true,
                email: true
            }
        },
        messages: {
            user: "Veuillez entrer un courriel valide"
        }
        });
    });
</script>

</head>
<body>

<h1 id="banner">Module Membre</h1>
<div id="main">

<form method="post" class="cmxform" id="form" action="formloginmember.php">
    <fieldset>
        <legend>Connexion</legend>
        <p>
            <label for="user">Courriel</label>
            <input id="user" name="user" class="required"/>
        </p>
        <p>
            <label for="pass">Password</label>
            <input type="password" name="password" id="password" class="required" minlength"5" />
        </p>
        <p>
            <input class="submit" type="submit" value="Login"/>
        </p>
    </fieldset>
    <input type="hidden" name="type" value="profile">
</form>

<div id="result"></div>
formloginmember.php

Code: Select all

<?php
session_start();
ob_start();
include("config.php");
usleep(500000);
$MemberEmail = $_REQUEST['user'];
$MemberPassword = md5($_REQUEST['password']);
$LoginType = $_REQUEST['type'];

$r = connectMember($MemberEmail, $MemberPassword);
if ($r == 1)
{
    echo "<font color=green>Connexion r&egrave;ussi</font>";
}
else
{
    echo "<font color=red>&Eacute;chec de connexion</font>";
}
if ($LoginType == "profile")
{
    header("Location: index.html");
}
Thanks !

Re: Question about a redirection with jQuery

Posted: Sun Jun 27, 2010 9:06 am
by Eran
1. You can't send headers after output (in your case, the echo occurs before the headers)
2. AJAX headers don't affect the current page (otherwise AJAX wouldn't work as intended). You can redirect in javascript using window.location = url;
https://developer.mozilla.org/en/window.location

Re: Question about a redirection with jQuery

Posted: Sun Jun 27, 2010 9:13 am
by ericblanc
oh okay thanks, but, can I call a php function from my jquery and check the return value, and do a window.location ?

Re: Question about a redirection with jQuery

Posted: Sun Jun 27, 2010 9:18 am
by Eran
try and see for yourself ;)

Re: Question about a redirection with jQuery

Posted: Sun Jun 27, 2010 11:14 am
by ericblanc
you can't help me with that ?

Re: Question about a redirection with jQuery

Posted: Sun Jun 27, 2010 11:20 am
by Eran
You aren't willing to try it yourself?

Re: Question about a redirection with jQuery

Posted: Sun Jun 27, 2010 2:04 pm
by ericblanc
ya, that's why i came here. Anyways, it seems I won't get any help. Thanks bro, have a nice day.