dongweng9474 2014-01-21 22:19
浏览 35
已采纳

在PHP中处理jQuery UI对话框表单

I've been searching around on this for a couple of days, but haven't found any answers that fit my particular problem. Some background:

I am fairly new at PHP, and am attempting to create user log-in functionality on a practice site I set up using HTML, CSS and jQuery. I have a form that is displayed by a jQuery UI dialog, with fields for username and password. What I want to do is have the "Log In" button in the dialog submit the form to a separate file, "login.php", for validation and processing. If the username exists in the MySQL database, I want it to return an error message that I can display in the same dialog. Same goes for if the username exists, but the password is wrong.

If the validation succeeds, I want the PHP file to set session variables, and redirect to index.php.

This may be a simple error, or a number of them on my part, but I do not know which direction to go at this point. Any assistance would be greatly appreciated. The code is as follows (header.php):

 <script>
    $(function() {
        var name = $("#userid"),
        password = $("#pw"),
        loginFields = $([]).add(name).add(password);

        $("#login_link").click(function() {
            $("#login_dialog").dialog("open"); 
        });

        $("#login_dialog").dialog({
            autoOpen: false,
            height: 200,
            width: 250,
            modal: true,
            buttons: {
                Submit: function() {                            
                            $("#login_form").submit();
                        },
                Cancel: function(){
                    $(this).dialog("close");
                }                
            },
            close: function() {
                loginFields.val("");
            }
        });

    });
</script>
    <div id="banner">
        <h1>Header</h1>
        <span id="user">
            <ul>
                <?php
                    //if user is logged in, show name and Log Out option, else show Log In and Sign Up options
                    if(isset($_SESSION["user"])) {
                        echo "<li>Welcome " . $_SESSION["fname"] . "!<li>";
                        echo "<li><a href='#'>Log Out</a></li>";
                    } else {
                        echo "<li><a href='#' id='login_link'>Log In</a></li>";
                        echo "<li><a href='#'>Sign up</a></li>";
                    }
                ?>
            </ul>
        </span>
    </div>

    <nav id="mainnav">
        <ul>
            <li><a href="#">One</a></li>
            <li><a href="#">Two</a></li>
            <li><a href="#">Three</a></li>
        </ul>
    </nav>

    <div id="login_dialog" title="Current User">
        <form id="login_form" action="login.php" method="post">
            <fieldset>
                <label for="userid">Username</label>
                <input type="text" name="userid" id="userid" />
                <label for="pw">Password</label>
                <input type="password" name="pw" id="pw" />
            </fieldset>
        </form>
    </div>

The PHP, right now a collection of interacting functions, is as follows:

<?php

function connect() {
    $con = mysqli_connect("localhost", "site", "blargh");

    if (mysqli_connect_errno()) {
        alert("Failed to connect to MySQL: " . mysqli_connect_error());
    }

    return $con;
}

function getUser($username, $password) {
    $con = connect();
    $result = mysqli_query($con, "SELECT * FROM practice_user
                           WHERE username = " . $username . " AND password = " . $password);

    return $result;
}

function setSessionUser($userData) {

    $_SESSION["username"] = $userData["username"];
    $_SESSION["fname"] = $userData["first_name"];
    $_SESSION["lname"] = $userData["last_name"];
}

//validation functions

function userExists($username, $password) {
    $con = connect();

    $result = getUser($username, $password);

    if (mysqli_num_rows($result) == 1) {
        return true;
    }
    else {
        return false;
    }
}



?>
  • 写回答

2条回答 默认 最新

  • doutan6286 2014-01-21 22:32
    关注

    If you are good with client side validation, you can do like:

    Submit: function() {                            
        if($("#login_form").valid()) {
            $("#login_form").submit();
        }                   
    }
    

    Else, if you are looking for server side validation as you mentioned by using login.php, you have to work out on that file as well:

    Submit: function() {                            
        validationCheck = $.post(
            'login.php', 
            {
                username:$('#userid').val(), 
                password:$('#pw').val()
            }
        );
        if(validationCheck) {
            $("#login_form").submit();
        }
    }
    

    PHP file:

    if( is_valid_username($_POST['userid']) 
        && is_valid_password($_POST['pw']) 
        && is_valid_user($_POST['userid'], $_POST['pw']) ) {
        echo json_encode(true);
        return false;
    }
    

    Assuming is_valid_username($_POST['userid']) validates the username, is_valid_password($_POST['pw']) validates the password and is_valid_user($_POST['userid'], $_POST['pw']) checks for valid user.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化