douyiyang6317 2014-06-05 17:37
浏览 28
已采纳

如何在php / Jquery(javascript)中创建一个指向另一个页面的按钮?

Hello I'm making a very basic "Login system" without sql database yet.

I want when I press "Start Session" it will start the session (log-in) and it'll redirect me to my second page(Home page) where a button "Destroy Session" (log-out) will apear, which will redirect back to the first page where you can log-in.

page1.php code (log in):

<html>
<head>

    <script src="http://code.jquery.com/jquery-latest.min.js"></script>

    <script>
        $(document).ready(function()
        {
        // For starting the session
            $('#start_session').click(function()
            {
                header("location:page2.php");
                $('#message').load('start_session.php')


            });
        });
    </script>

</head>

<body>
    <div id='message'></div>
    <input type="button" value="Start Session" id="start_session">
</body>

page2.php code (log out):

<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>

    <script>
        $(document).ready(function()
        {

        $('#destroy_session').click(function()
            {
                header("location:page1.php");
                $('#message').load('destroy_session.php')

            });
        });
    </script>

</head>

<body>
    <div id='message'></div>

    <input type="button" value="Destroy Session" id="destroy_session">
</body>

start_session.php code:

    <?php



session_start();



if(!isset($_SESSION['visits']))
{
    $_SESSION['visits'] = 0;
}

else
{
    $visits = $_SESSION['visits']++;

    if ($visits == 0)
    {
        echo "This is your first time on this site, isn'/t it? <br />";
    }

    else 
    {
        echo "You have visited this site: $visits times.<br />";
    }


    exit();

    echo ' You are logged in';  
}

?>

destroy_session.php code:

    <?php
session_start();
echo $_SESSION['visits'];
unset($_SESSION['visits']);


echo "You have been logged out";
?>

Note: I have tried header('Location: page.php'); but this will redirect me instantly to page 2 from page 1 and I want it on the click.

  • 写回答

1条回答 默认 最新

  • doudou6050 2014-06-05 17:43
    关注

    In javascript your redirect using window.location.href:

    $('#start_session').click(function() {
        $('#message').load('start_session.php', function() {
            window.location.href = 'page2.php';
        });
    });
    

    And you should redirect when you have processed your php so I put it in the callback function.

    Note that you will not see the results that your php outputs because of the redirect so you should probably include that output in the next page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算