如何在颜色框中使用php ajax表单?

如何在颜色框中使用php ajax表单?,php,javascript,jquery,forms,colorbox,Php,Javascript,Jquery,Forms,Colorbox,我在login.php中有一个登录表单。它通过ajax调用单独的sample.php,sample.php返回值,Javascript将根据php返回值显示相关消息。它在一个普通的网页上工作得非常好。但是当我在test.php的颜色框中显示表单时。javascript/jquery无法运行。我已经使用ajaxform对此进行了一些研究,但具体如何做呢?请给我提供一些关键词,以便我进一步研究:,我被绊倒了 test.php: $(".ajax").colorbox(); <a href=""

我在login.php中有一个登录表单。它通过ajax调用单独的sample.php,sample.php返回值,Javascript将根据php返回值显示相关消息。它在一个普通的网页上工作得非常好。但是当我在test.php的颜色框中显示表单时。javascript/jquery无法运行。我已经使用ajaxform对此进行了一些研究,但具体如何做呢?请给我提供一些关键词,以便我进一步研究:,我被绊倒了

test.php:

$(".ajax").colorbox();
<a href="" class=ajax>Login</a>
这是我的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.'"}';
    }
?>

您需要在表单标记中更改的第一件事:

赖特只写上面的代码。因为您已经给出了方法和URL,所以它会刷新页面

替换jquery代码$'loginForm'。提交函数{此代码替换为$'.login'。单击函数{

为什么在ajax成功函数中使用replace方法是因为给出了错误。删除else部分

location对象没有替换方法。如果成功后要重定向到用户,请使用location.href=your link;。
进行这些更改,然后检查。

那么代码中的错误是什么?问题出在哪里。javascript/jquery不会在colorbox中执行。当我单击“提交”时,它只会返回到login.php,但在colorbox之外工作得很好。从ajax.hmm中的代码中删除else部分else location.replacemsg.txt;我认为javascript永远不会运行好的。你可以在这里看到演示。当你在每张图片上点击勾号时,登录颜色框会显示出来。但是当你访问时,它工作正常。嗨,我已经重新编辑了帖子并添加了login_submit.php。我想jquery根本就没有运行过,当我点击它时,例如,prevent default never run.btw,除了使用location.href之外还有其他方法吗?因为我我正在使用login_submit.php运行逻辑并返回不同的链接。@conglong如果jquery没有运行,那么颜色框是如何打开的。这意味着jquery正在工作。是的,我的意思是,当我打开颜色框时,有一个正确的登录表单,当密码不正确/字段为空时,jquery实际上会显示相关的错误信息表单中的年龄。这在我直接打开登录表单时有效,但在颜色框中打开时无效。这意味着在颜色框打开后我正在运行jquery
<?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.'"}';
    }