0

理由はわかりませんが、AJAX が失敗していることを確認するために「エラー」句を追加しました。AJAX部分まで正しく動作しますが、データを送信していないだけで、理由がわかりません。

<script type="text/javascript">
<!--
$(document).ready(function() {

$("#login").click(function() {

    document.getElementById("result").innerHTML = 'Validating credentials...';

    var un = $("#un").val();
    var pw = $("#pw").val();

    if ( un == "" )
    {
        document.getElementById("un_error").style.visibility = 'visible';
        $("#un").focus();
    }

    if ( pw == "" )
    {
        document.getElementById("pw_error").style.visibility = 'visible';
        $("#pw").focus();
    }

    $.ajax({
        type: 'POST',
        url: 'login-parse.php',
        data: { un: un, pw: md5(pw) },
        success: function(msg) {
                document.getElementById("result").innerHTML = msg;
                    },
        error: function(xhr, status) { alert(status); }
    });

});

});
//-->
</script>

それがJSコードです。

これはHTMLです:

    <div id="content">
    <div id="result" class="result"></div>
    <h2>Login To Your Account</h2>
    <div class="text">
        <fieldset>
            <fieldset>
                <legend>Username</legend>
                <input type="text" id="un" value="" size="20" /><span class="error" id="un_error">*</span>
            </fieldset>
            <fieldset>
                <legend>Password</legend>
                <input type="password" id="pw" value="" size="30" /> <span class="error" id="pw_error">*</span>
            </fieldset>
            <input type="button" id="login" value="Login" />
        </fieldset>
    </div>
</div>

<?php

// Login Parser

require 'inc.common.php';

if (! isset ( $_POST['un'], $_POST['pw']) )
{
    echo '<blockquote>Invalid username/password combination.</blockquote>' . "\n";
} else {
    $un = $_POST['un'];
    $pw = md5($_POST['pw']);

    $check = $sql->result ( $sql->query ( 'SELECT COUNT(*) FROM `users` WHERE `user_name` = \'' . $sql->escape($un) . '\' AND `user_password` = \'' . $sql->escape($pw) . '\'' ) );

    $errors = array();

    if (! strlen ( $un ) )
        $errors[] = 'Please enter a valid username.';

    if (! strlen ( $pw ) )
        $errors[] = 'Please enter a valid password.';

    if ( $check == 0 )
        $errors[] = 'Invalid username/password combination.';

    if ( count ( $errors ) > 0 )
    {
        echo '<blockquote>' . "\n",
             '  The following errors occurred with your login:' . "\n",
             '  <ul>' . "\n";
        foreach ( $errors as $enum => $error )
        {
            echo '      <li><strong>(#' . ($enum+1) . '):</strong> ' . $error . '</li>' . "\n";
        }
        echo '  </ul>' . "\n",
             '</blockquote>' . "\n";
    } else {
        setcookie ( 'ajax_un', $un, time()+60*3600 );
        setcookie ( 'ajax_pw', $pw, time()+60*3600 );

        echo '<blockquote>' . "\n",
             '  <p><strong>Success!</strong></p>' . "\n",
             '  <p>You have successfully been logged in as <strong>' . $un . '</strong>.</p>' . "\n",
             '  <p>You may now return to the <a href="index.php">index page</a>.</p>' . "\n",
             '</blockquote>' . "\n";

    }

}

?>
4

1 に答える 1