Question about a redirection with jQuery

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
ericblanc
Forum Newbie
Posts: 4
Joined: Sun Jun 27, 2010 8:39 am

Question about a redirection with jQuery

Post 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 !
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Question about a redirection with jQuery

Post 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
ericblanc
Forum Newbie
Posts: 4
Joined: Sun Jun 27, 2010 8:39 am

Re: Question about a redirection with jQuery

Post 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 ?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Question about a redirection with jQuery

Post by Eran »

try and see for yourself ;)
ericblanc
Forum Newbie
Posts: 4
Joined: Sun Jun 27, 2010 8:39 am

Re: Question about a redirection with jQuery

Post by ericblanc »

you can't help me with that ?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Question about a redirection with jQuery

Post by Eran »

You aren't willing to try it yourself?
ericblanc
Forum Newbie
Posts: 4
Joined: Sun Jun 27, 2010 8:39 am

Re: Question about a redirection with jQuery

Post by ericblanc »

ya, that's why i came here. Anyways, it seems I won't get any help. Thanks bro, have a nice day.
Post Reply