1

我在(login.php)中有一个登录表单。它通过 ajax 调用一个单独的 sample.php,sample.php 返回值,Javascript 将根据 php 返回值显示相关消息。它在普通网页中运行良好。但是当我在彩色框中显示表单时(在 test.php 中)。javascript/jquery 无法运行。我通过使用 ajaxform 对此进行了研究,但我到底该怎么做呢?请告诉我一些关键字让我进一步研究:(,我被困住了。

测试.php:

$(".ajax").colorbox();
<a href="" class=ajax>Login</a>

这是我的 ajax 函数:

function login()
{
hideshow('loading',1);
error(0);

$.ajax({
    type: "POST",
    url: "http://utourpia.me/php/login_submit.php",     
    data: $('#loginForm').serialize(),
    dataType: "json",
    success: function(msg){

        if(!(msg.status))
        {
            error(1,msg.txt);
        }
        else location.replace(msg.txt);

        hideshow('loading',0);
    }
});
}

这是我的 jQuery:

$('#loginForm').submit(function(e) {
            login();
            e.preventDefault(); 
        });

这是我的表格:

<form id=loginForm method=post action="">

<label for=email class=email>Email:</label>
<input name=email type=text size=20 maxlength=40/>

<label for="password" class="password">Password:</label>
<input name="password" type="password" size="20" maxlength="40" />

<input class="login" type="submit" name="submit" value="Login" />
</form>

<div id="error"></div>

login_submit.php

<?php
require_once('../lib/connections/db.php');
include('../lib/functions/functions.php');
session_start();
$location_id = $_SESSION['location_id'];

$email = $_POST['email'];

$query = mysql_query('SELECT username FROM users WHERE email = "'.secureInput($email).'"') or die (mysql_error());

        if(mysql_num_rows($query) == 1)
        {
            $row = mysql_fetch_assoc($query);
            $username = $row['username'];
        }

$returnURL1 = 'http://utourpia.me/php/places.php?id='.$location_id.'&username='.$username;
$returnURL2 = 'http://utourpia.me/php/myprofile.php?username='.$username;
$returnURL3 = 'http://utourpia.me';
$returnURL4 = 'http://utourpia.me/php/dreamtrip.php';

//For login

    // we check if everything is filled in and perform checks

    if(!$_POST['email'] || !$_POST['password'])
    {
        die(msg(0,"Email and / or password fields empty!"));
    }

    else
        {
            $res = login($_POST['email'],$_POST['password'],$username);
                if ($res == 1){
                    die(msg(0,"Email and / or password incorrect!"));
                }
                if ($res == 2){
                    die(msg(0,"Sorry! Your account has been suspended!"));
                }
                if ($res == 3){
                    die(msg(0,"Sorry! Your account has not been activated. Please check your email's inbox or spam folder for a link to activate your account."));
                }
                if ($res == 99){

                    if ($_SESSION['login_submit']=="places.php")
                    {
                    echo(msg(1,$returnURL1));
                    }
                    else if ($_SESSION['login_submit']=="myprofile.php")
                    {
                    echo(msg(1,$returnURL2));
                    }
                    else if ($_SESSION['login_submit']=="home.php")
                    {
                    echo(msg(1,$returnURL3));
                    }
                    else if ($_SESSION['login_submit']=="dreamtrip.php")
                    {
                    echo(msg(1,$returnURL4));
                    }
                }
        }

    function msg($status,$txt)
    {
        return '{"status":'.$status.',"txt":"'.$txt.'"}';
    }

?>

4

1 回答 1

0
  1. 您需要在表单标签中更改的第一件事:

    赖特只在上面的代码。因为您已经给出了刷新页面的方法和 URL。

  2. 替换您的 jquery 代码$('#loginForm').submit(function(e) {,此代码替换为$('.login').click(function(e) {.

  3. 你为什么在ajax成功函数中使用该replace方法会给出错误。删除其他部分。

  4. location 对象没有 replace 方法。如果您想在成功时重定向到用户,请使用location.href="your link";.

进行这些更改,然后检查。

于 2013-07-17T12:24:25.820 回答