0

私のindex.phpページには次のようなものがあります:

if(isset($_GET['page'])) {
        switch ($_GET['page']) {
            case 'login':
                include('login.php');
            break;
            case 'log_out':
                $_SESSION['loggedin'] = false;
                include('loggedout.html');
            break;
            case 'profile':
                include('profile.php');
            break;
            case 'contact_us':
                include('contact.html');
            break;
            default:
                include('home.html');
            break;
        }
        }  else {
            include('home.html'); 
        }

リンクが押されたとき、またはフォームが送信されたときに、新しいページを簡単に呼び出すことができるようになりました。

<a href="index.php?page=home">Home</a> <a href="index.php?page=contact_us">Contact Us</a> 

しかし、もし私がオールインワンのフォームを持っていたとしたら...index.phpログインしたページを表示したり、login.phpページを再表示したりするには、PHPコードを介してリクエストを送信できる必要がありますが、それを可能にするものを見つけることができないようですphp スクリプトでリクエストを送信します。

<?php 
    if(..) {
        send request to index.php i.e index.php?page=profile
    }
?>

これが必要な理由は、フォーマットとしてindex.php使用するためです。divsしたがって、ページをリクエストしないとindex.php、新しい HTML で表示されます (つまり、ナビゲーション バーなどはありません)。

@HydraIOなどのコメントによると、JQueryを使用してこれを行うにはどうすればよいですか?

ここにいくつかのコードがあります:

lgoin.php:

<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <title>Login</title>
        <meta http-equiv="content-type"
        content="text/html; charset=iso-8859-1"
        />
        <!--
            //thanks http://decoraora.com/fototapete_115x175.php for the image http://decoraora.com/fototapete/images/664.jpg -->
        <style type="text/css">
        .table { 
            background: url("bg.jpg") no-repeat; 
            width: 600px;
            height: 250px;
        }
        </style>
    </head>

    <body>
        <?php           
            if (isset($_POST['formLogin'])) {
                    $un = $_POST['username'];
                    $pwd = $_POST['password'];
                    if($pwd!=''&&$un!='') {
                        if (md5($pwd)==='29ef52e7563626a96cea7f4b4085c124') {
                            $_SESSION['loggedin'] = true;
                            $_SESSION['login_error'] = 0;
                            $_SESSION['username'] = $un;
                            $_SESSION['password'] = $pwd;
                            //include('profile.php');
                            //header("Location: index.php/page=profile");
                        //header("Location: http://localhost/index.php?index.php?page=profile");

                        } else {
                            $_SESSION['login_error'] = 1;
                            echo 'this will dipslay in its own page';
                            //include('login.php');
                        }
                    }else {
                        $_SESSION['login_error'] = 2;
                            echo 'display in its own page';
                        //include('login.php');

        ?>
        <form action="login.php" method="post">
            <table bgcolor="white" align="center" border="1px" width="50%" style="height: 40%;">
                <tr>
                    <td colspan="2" align="center">Login for CC Chat</td>
                </tr>
                <tr>
                    <td>Username:</td>
                    <td>Password:</td>
                </tr>
                <tr>
                    <td><input type="text" name="username" maxlength="50"/></td>
                    <td><input type="password" name="password" maxlength="50"/></td>
                </tr>
                <tr>
                    <td><a href="index.php?page=contact_us">Forgot your password?</a></td>
                    <td><input type="submit" name="formLogin" value="Login"/></td>
                </tr>
                <?php 
                    if(isset($_SESSION['login_error'])) {
                        switch ($_SESSION['login_error']) {
                            case 1:
                                echo '<tr>';
                                echo '<td>';
                                echo '<font color="red">Password does not match</font>';
                                echo '</td>';
                                echo '</tr>';
                                $_SESSION['login_error']=0;
                            break;
                            case 2:
                                echo '<tr>';
                                echo '<td>';
                                echo '<font color="red">Password and/or Username cannot be empty</font>';
                                echo '</td>';
                                echo '</tr>';
                                $_SESSION['login_error']=0;
                            break;
                            default:
                            break;
                        }
                    }
                    ?>
                <tr>
                    <td colspan="2" align="center">&copy; CopyLeft David Kroukamp  11039662 2013 - 2015</td>
                </tr>
            </table>
        </form>
        <?php 
        }
        }else {
        ?>
        <form action="login.php" method="post">
            <table bgcolor="white" align="center" border="1px" width="50%" style="height: 40%;">
                <tr>
                    <td colspan="2" align="center">Login for CC Chat</td>
                </tr>
                <tr>
                    <td>Username:</td>
                    <td>Password:</td>
                </tr>
                <tr>
                    <td><input type="text" name="username" maxlength="50"/></td>
                    <td><input type="password" name="password" maxlength="50"/></td>
                </tr>
                <tr>
                    <td><a href="index.php?page=contact_us">Forgot your password?</a></td>
                    <td><input type="submit" name="formLogin" value="Login"/></td>
                </tr>
                <?php 
                    if(isset($_SESSION['login_error'])) {
                        switch ($_SESSION['login_error']) {
                            case 1:
                                echo '<tr>';
                                echo '<td>';
                                echo '<font color="red">Password does not match</font>';
                                echo '</td>';
                                echo '</tr>';
                                $_SESSION['login_error']=0;
                            break;
                            case 2:
                                echo '<tr>';
                                echo '<td>';
                                echo '<font color="red">Password and/or Username cannot be empty</font>';
                                echo '</td>';
                                echo '</tr>';
                                $_SESSION['login_error']=0;
                            break;
                            default:
                            break;
                        }
                    }
                    ?>
                <tr>
                    <td colspan="2" align="center">&copy; CopyLeft David Kroukamp  11039662 2013 - 2015</td>
                </tr>
            </table>
        </form>
        <?php
        }
        ?>
    </body>

</html>

index.php:

<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>CC Chat</title>
        <meta 
        http-equiv="content-type" content="text/html; charset=iso-8859-1" 
        />
        <!-- 
        Thanks too http://stackoverflow.com/a/7851347/1133011 for the help
        on layout which acts more like frames but avoids them and using divs. As frames have 
        known draw backs see here http://stackoverflow.com/questions/4600648/frames-with-php we
        should thus rather use include in php
        !-->
        <style type="text/css" media="screen">
        /* Generic pane rules */
            body { margin: 0 }
            .row, .col { overflow: hidden; position: absolute; }
            .row { left: 0; right: 0; }
            .col { top: 0; bottom: 0; }
            .scroll-x { overflow-x: auto; }
            .scroll-y { overflow-y: auto; }

            .header.row { height: 75px; top: 0; background-color: #E5E5E5; }
            .menu.row { height: 75px; top: 75px; background-color: #EDEDED;  }
            .body.row { top: 150px; bottom: 50px; background-color: #F5F5F5; }
            .footer.row { height: 75px; bottom: 0; background-color: #EBEBEB; }

            A:link {text-decoration: none}
            A:visited {text-decoration: none}
            A:active {text-decoration: none}
            A:hover {font-size:24; font-weight:bold; color: red;}
        </style>
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    </head>
    <body>
        <div class="header row">
            <?php 
                include("header.html"); 
            ?>
        </div> 
        <div class="body row scroll-y">
            <?php
        if(isset($_GET['page'])) {
                        switch ($_GET['page']) {
                            case 'login':
                                include('login.php');
                            break;
                            case 'log_out':
                                $_SESSION['loggedin'] = false;
                                include('loggedout.html');
                            break;
                            case 'profile':
                                include('profile.php');
                            break;
                            case 'contact_us':
                                include('contact.html');
                            break;
                            default:
                                include('home.html');
                            break;
                        }
                }  else {
                    include('home.html'); 
                }
            ?>
        </div> 
        <div class="menu row">
            <?php 
                include("nav.php"); 
            ?>
        </div> 
        <div class="footer row">
            <?php 
                include("footer.php"); 
            ?>
        </div>
    </body>
</html>

パスワード ハッシュをチェックする if ステートメントでの JQuery 試行による更新:

<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <title>Login</title>
        <meta http-equiv="content-type"
        content="text/html; charset=iso-8859-1"
        />
        <!--
            //thanks http://decoraora.com/fototapete_115x175.php for the image http://decoraora.com/fototapete/images/664.jpg -->
        <style type="text/css">
        .table { 
            background: url("bg.jpg") no-repeat; 
            width: 600px;
            height: 250px;
        }
        </style>    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

    </head>

    <body>
        <?php           
            if (isset($_POST['formLogin'])) {
                    $un = $_POST['username'];
                    $pwd = $_POST['password'];
                    if($pwd!=''&&$un!='') {
                        if (md5($pwd)==='29ef52e7563626a96cea7f4b4085c124') {
                            $_SESSION['loggedin'] = true;
                            $_SESSION['login_error'] = 0;
                            $_SESSION['username'] = $un;
                            $_SESSION['password'] = $pwd;
                            //include('profile.php');
                            //header("Location: index.php/page=profile");
                            //header("Location: http://localhost/index.php?index.php?page=profile");
                            //exit();
        echo "<script type='text/javascript'>";
        echo "$.get('index.php?page=profile', function(data) {
  $('.body.row').html(data);
  alert('Load was performed.');
});";  
echo "</script>";
                        } else {
                            $_SESSION['login_error'] = 1;
                            echo 'this will dipslay in its own page';
                            //include('login.php');
                        }
                    }else {
                        $_SESSION['login_error'] = 2;
                            echo 'display in its own page';
                        //include('login.php');

        ?>
        <form action="login.php" method="post">
            <table bgcolor="white" align="center" border="1px" width="50%" style="height: 40%;">
                <tr>
                    <td colspan="2" align="center">Login for CC Chat</td>
                </tr>
                <tr>
                    <td>Username:</td>
                    <td>Password:</td>
                </tr>
                <tr>
                    <td><input type="text" name="username" maxlength="50"/></td>
                    <td><input type="password" name="password" maxlength="50"/></td>
                </tr>
                <tr>
                    <td><a href="index.php?page=contact_us">Forgot your password?</a></td>
                    <td><input type="submit" name="formLogin" value="Login"/></td>
                </tr>
                <?php 
                    if(isset($_SESSION['login_error'])) {
                        switch ($_SESSION['login_error']) {
                            case 1:
                                echo '<tr>';
                                echo '<td>';
                                echo '<font color="red">Password does not match</font>';
                                echo '</td>';
                                echo '</tr>';
                                $_SESSION['login_error']=0;
                            break;
                            case 2:
                                echo '<tr>';
                                echo '<td>';
                                echo '<font color="red">Password and/or Username cannot be empty</font>';
                                echo '</td>';
                                echo '</tr>';
                                $_SESSION['login_error']=0;
                            break;
                            default:
                            break;
                        }
                    }
                    ?>
                <tr>
                    <td colspan="2" align="center">&copy; CopyLeft David Kroukamp  11039662 2013 - 2015</td>
                </tr>
            </table>
        </form>
        <?php 
        }
        }else {
        ?>
        <form action="login.php" method="post">
            <table bgcolor="white" align="center" border="1px" width="50%" style="height: 40%;">
                <tr>
                    <td colspan="2" align="center">Login for CC Chat</td>
                </tr>
                <tr>
                    <td>Username:</td>
                    <td>Password:</td>
                </tr>
                <tr>
                    <td><input type="text" name="username" maxlength="50"/></td>
                    <td><input type="password" name="password" maxlength="50"/></td>
                </tr>
                <tr>
                    <td><a href="index.php?page=contact_us">Forgot your password?</a></td>
                    <td><input type="submit" name="formLogin" value="Login"/></td>
                </tr>
                <?php 
                    if(isset($_SESSION['login_error'])) {
                        switch ($_SESSION['login_error']) {
                            case 1:
                                echo '<tr>';
                                echo '<td>';
                                echo '<font color="red">Password does not match</font>';
                                echo '</td>';
                                echo '</tr>';
                                $_SESSION['login_error']=0;
                            break;
                            case 2:
                                echo '<tr>';
                                echo '<td>';
                                echo '<font color="red">Password and/or Username cannot be empty</font>';
                                echo '</td>';
                                echo '</tr>';
                                $_SESSION['login_error']=0;
                            break;
                            default:
                            break;
                        }
                    }
                    ?>
                <tr>
                    <td colspan="2" align="center">&copy; CopyLeft David Kroukamp  11039662 2013 - 2015</td>
                </tr>
            </table>
        </form>
        <?php
        }
        ?>
    </body>

</html>

アラートが表示されますが、以前のように空白の html にリダイレクトされます (少なくともフォームがリロードされる前よりも)

4

2 に答える 2

0

最初にインデックス ページ内のすべてのデータを評価し、php 内のヘッダーを同じインデックス ページに設定してページ リダイレクトを行い、get クエリを必要に応じて更新する必要があるようです。

header("Location: http://www.example.com/index.php?page=profile");
exit();

リダイレクトを行う前に、エコーなどの方法で html を出力しないようにしてください。その前にスクリプトがエコーを頻繁に使用する場合は、エコーの出力バッファリングを調べることができます。

于 2013-04-11T17:58:00.480 に答える
-1

PHP はサーバー側の言語であり、動的ではありません。JavaScript(私の例ではjQuery)を使用したい。

$_GET = <?php echo json_encode($_GET); ?>;
if($_GET['val'] == some_value){
  $.get(index.php?getvar=getval, {
    complete: function(){
      $('#div').val("YOUR OUTPUT"); // *
    }
  });
}

*これは任意のコードでかまいません。必要に応じて、div を php ファイル全体に置き換えることができます。

于 2013-04-11T18:08:24.467 に答える