dp152153 2014-01-23 03:51
浏览 14
已采纳

PHP登录使用2d数组,并且使用$ .get或$ .post的jquery不刷新页面

I got here a working code that returns the corresponding user_id of the user when he logs in. But then, I need to use the $.get or $.post of jquery so that there will be no page refresh. I read posts about using $.get and $.post, but I doesn't seem to understand them.

Here is my working code. Please help me apply $.get or $.post to this. thanks!

<?php

    $login = array
        ( 
               0=>array
                    ( 'username'=>'Art', 
                      'password'=>'p@ssw0rd',
                      'user_id'=>'1'              
                    ),
               1=>array
                    ( 'username'=>'Berto',
                      'password'=>'1234',
                      'user_id'=>'2'
                    ),
               2=>array
                    ( 'username'=>'Carrie',
                      'password'=>'5678',
                      'user_id'=>'3'
                    ),
               3=>array
                    ( 'username'=>'Dino',
                      'password'=>'qwer',
                      'user_id'=>'4'
                    ),
               4=>array
                    ( 'username'=>'Ely',
                      'password'=>'asdf',
                      'user_id'=>'5'
                    )
             );          

  if (isset($_POST['submit']))
  {
    $found_user = false;
    $uname = $_POST['user'];
    $pass = $_POST['password'];

    foreach ($login as $sub_array) {
        if ($uname == $sub_array['username'] && $pass == $sub_array['password']) {
            $found_user = true;
            $message = $sub_array['user_id'];
            break;
        }
    }

        if ($found_user) {
            $message = ( $sub_array['user_id'] );
            echo "<script type='text/javascript'>
            alert('Your user ID is : $message');
            </script>";

        } else {
            echo "<script type='text/javascript'>
            alert('Invalid username/password.');
            </script>";
        }
}

?>

<body>
<form action="" method="post">
<label for="name"><span class="nameLabel">Username: </span></label>
<span class="nameLabel">
<input name="user" type="text" class="name" id="user" />
</span>
<label for="pass" class="passwordLabel">Password: </label>
<input name="password" type="password" class="password" id="password" />
<input name="submit" type="submit" class="logIn" id="login" value="Log In"/>
</form>
</body>
  • 写回答

2条回答 默认 最新

  • dsj83686 2014-01-23 04:10
    关注

    You need ajax for fulfill your requirement:

    1) You need to separate php login functionalities in a separate php file.
    2) you need to make an ajax call when clicking in login button. you need to add some javascript for your ajax call.
    3) add jquery in your html file

    Your php file( i have named it t2.php)

    $login = array
    (
        0=>array
        ( 'username'=>'Art',
            'password'=>'p@ssw0rd',
            'user_id'=>'1'
        ),
        1=>array
        ( 'username'=>'Berto',
            'password'=>'1234',
            'user_id'=>'2'
        ),
        2=>array
        ( 'username'=>'Carrie',
            'password'=>'5678',
            'user_id'=>'3'
        ),
        3=>array
        ( 'username'=>'Dino',
            'password'=>'qwer',
            'user_id'=>'4'
        ),
        4=>array
        ( 'username'=>'Ely',
            'password'=>'asdf',
            'user_id'=>'5'
        )
    );
    
    
        $found_user = false;
        $uname = $_POST['user'];
        $pass = $_POST['password'];
    
        foreach ($login as $sub_array) {
            if ($uname == $sub_array['username'] && $pass == $sub_array['password']) {
                $found_user = true;
                $message = $sub_array['user_id'];
                break;
            }
        }
    
        if ($found_user) {
            $message = ( $sub_array['user_id'] );
    
               echo "Your user ID is : $message";
    
    
        } else {
                echo 'Invalid username/password.';
        }
    

    in your html file:

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <form action="" method="post" id="login-form">
        <label for="name"><span class="nameLabel">Username: </span></label>
    <span class="nameLabel">
    <input name="user" type="text" class="name" id="user" />
    </span>
        <label for="pass" class="passwordLabel">Password: </label>
        <input name="password" type="password" class="password" id="password" />
        <input name="submit" type="button" class="logIn" id="login" value="Log In"/>
    </form>
    
    <script type="text/javascript">
        $( document ).ready(function() {
           $("#login").click(function(e){
               e.preventDefault();
               $.ajax({
                   url: "t2.php",
                   type: "post",
                   data: $("#login-form").serialize(),
                   success: function(data){
                       alert(data);
                   }
               })
           })
        });
    </script>
    

    You need to learn basic of ajax-php-jquery: http://blog.teamtreehouse.com/beginners-guide-to-ajax-development-with-php

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

报告相同问题?

悬赏问题

  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥15 小红薯封设备能解决的来
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答