使用AJAX(JavaScript)加载页面& PHP

时间:2015-10-25 13:51:55

标签: javascript php jquery html ajax

我是Ryan而且我被困了......我想构建一个管理页面,您可以在不加载同步的情况下在页面之间切换(使用AJAX)......但这不是问题,问题是我我使用JavaScript(jQuery)来调整元素大小,然后我使用JavaScript(AJAX)加载一个php脚本异步... php-script也有不同的元素,其中JavaScript(主要是jQuery)实现...所以JavaScript工作,然后php工作,但JavaScript拒绝工作后!我怎么能这样做?

这就是它的样子: layout of the admin page

的index.php:

<?php
    session_start ();
    require ('php/content.php');

    if (!isset($_SESSION["admin"])) {
        $_SESSION["admin"] = false;
    }
?>

<!DOCTYPE html>
<html>
    <head>
        <title>Administratiepagina van Polen</title>
        <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
        <?php if ($_SESSION["admin"]) {
            echo '<script type="text/javascript" src="JavaScript/page.js"></script>';
            echo '<script type="text/javascript" src="stylesheets/admin_style.js"></script>';
        } else {
            echo '<script type="text/javascript" src="stylesheets/login_style.js"></script>';
        }    ?>
    </head>
    <body id="body" >
        <?php load_page (); ?>
    </body>
</html>

php - &gt; content.php:

<?php

    function load_page () {

        if ($_SESSION["admin"] !== true) {
            login_content ();
        } else {
            page_content ();
        }

    }

    function login_content () {

        if ( isset($_SESSION["lock"]) && ($_SESSION["lock"] - time()) > 0 ) {

            // still in progress...
            echo "<div class='login-box'>";
            echo "<div class='title'><h1>administratie</h1></div>";
            echo "<h1>U moet nog " . ($_SESSION["lock"] - time()) . " seconden wachten!";
            echo "</div>";

        } else {

            // require login.php
            require_once ('login.php');

            // standaard login form
            echo "<form action='' method='POST' class='login-box' autocomplete='off' autocorrect='off' autocapitalize='off' >";
            echo "<div class='title'><h1>administratie</h1></div>";
            echo "<div class='login' >";
            echo "<input name='username' class='username' type='text' placeholder='gebruikersnaam'/> ";
            echo "<input name='password' class='password' type='password' placeholder='wachtwoord'/> ";

            if (isset($_SESSION["error"])) {
                echo "<p class='error' >* " . $_SESSION["error"] .  "</p>";
            }

            echo "<input name='login' class='login-button' type='submit' value='Inloggen'/> ";

            echo "</div>";
            echo "</form>";

        }

    }

    function page_content () {

        /*** variables ***/
        if (!isset($_POST["tab"]))
            $_POST["tab"] = "Startpagina";

        /*** header ***/
        echo "<div class='header' >";

        /*** require logout.php ***/
        require_once ('logout.php');

        echo "<form action='' method='POST' class='logout' >";
        echo "<input type='submit' value='Log uit' name='logout' >";
        echo "</form>";
        echo "</div>";

        /*** main div ***/
        echo "<div class='total-frame' >";

        /*** main menu ***/
        echo "<form action='' method='POST' class='list-menu' >";

        echo "<input type='submit' value='Startpagina' name='tab' />";
        echo "<input type='submit' value='Agenda' name='tab' />";
        echo "<input type='submit' value='Klanten' name='tab' />";
        echo "<input type='submit' value='Personeel' name='tab' />";
        echo "<input type='submit' value='Website' name='tab' />";
        echo "<input type='submit' value='Documenten' name='tab' />";
        echo "<input type='submit' value='Instellingen' name='tab' />";

        echo "</form>";

        /*** main content ***/
        echo "<div class='mainframe'>";
        echo    "<div class='frame-header'>";
        echo        "<h1>" . $_POST["tab"] . "</h1>";
        echo        "<nav>";
        echo            "<ul>";

        echo                "<li><input type='submit' value='' name='tab' /></li>";

        echo            "</ul>";
        echo        "</nav>";
        echo    "</div>";
        echo    "<div class='frame-content'>";

        echo    "</div>";
        echo "</div>";

    }

?>

php - &gt;的login.php:

<?php

    if (isset($_POST["login"])) {
        login ();
    }

    function login () {

        // ontvangen variabelen
        $_username  = $_POST["username"];
        $_password  = $_POST["password"];

        // benodigde variabelen van de database
        $_server    = "something.host.com";
        $_user      = "user";
        $_pass      = "pass";
        $_database  = "database";

        // connectie maken met de database
        $_connection = mysqli_connect($_server, $_user, $_pass, $_database);

        // de input velden escapen
        $_username  = mysqli_real_escape_string($_connection, $_username);
        $_password  = mysqli_real_escape_string($_connection, $_password);

        unset ($_SESSION["error"]);

        // controle: waardes zijn niet gelijk aan NULL
        if (!isset($_username) || !isset($_password)) {
            Error ("Alle velden zijn verplicht!");
        } else {

            // controle: voldoen de ingevulde waarden aan de eisen
            $_pattern = "/^[A-Za-z0-9!@#%&*]{6,20}$/";

            if (!preg_match($_pattern, $_username) || !preg_match($_pattern, $_password)) {
                Error ("Vul re&#235;le waarden in!");
            } else {

                // controle: verbinding met de database
                if (!$_connection) {
                    Error ("Geen verbinding!");
                } else {

                    // verbinden met de database
                    $_sql = "SELECT * FROM users WHERE username = '$_username' AND password = MD5('$_password') AND admin = 1 ";
                    $_result = mysqli_query($_connection, $_sql);

                    // controle: feedback is van de database
                    if (!$_result) {
                        Error ("Geen feedback van de database!");
                    } else {

                        // controle: bestaan van het account
                        if(mysqli_num_rows($_result) != 1) {
                            Error ("Dit account bestaat niet!");
                        } else {
                            $_SESSION["admin"] = true;
                            header('location: http://admin.vanpolencv.nl');
                            return;
                        }
                    }
                }
            }
        }
    }

    function Error ($_message) {
        $_SESSION["error"] = $_message;
        return;
    }

?>

php - &gt; logout.php:

<?php

    if (isset($_POST["logout"])) {
        logout ();
    }

    function logout () {

        // benodigde variabelen van de database
        $_server    = "something.host.com";
        $_user      = "user";
        $_pass      = "pass";
        $_database  = "database";

        // connectie maken met de database
        $_connection = mysqli_connect($_server, $_user, $_pass, $_database);

        unset ($_SESSION["error"]);

        session_destroy ();
        header('location: http://admin.vanpolencv.nl');

        return;

    }

    function Error ($_message) {
        $_SESSION["error"] = $_message;
        return;
    }

?>

php - &gt; page.php文件:

<?php

    if (!isset($_REQUEST["p"])) {
        error ();
    } else {
        load_content ($_REQUEST["p"]);
    }


    function load_content ($_name) {

        switch ($_name) {

            case "home":

                echo "";
                echo "";
                echo "";
                echo "";

                break;

            case "agenda":

                echo "";
                echo "";
                echo "";
                echo "";

                break;

            case "klanten":

                echo "";
                echo "";
                echo "";
                echo "";

                break;

            case "personeel":

                echo "<h1>HALLO</h1>";
                echo "";
                echo "";
                echo "";

                break;

            case "website":



                // benodigde variabelen van de database
                $_server    = "something.host.com";
                $_user      = "user";
                $_pass      = "pass";
                $_database  = "database";

                // connectie maken met de database
                $_connection = mysqli_connect($_server, $_user, $_pass, $_database);

                echo "<div class='web_builder'>";

                $_sql       = "SELECT titel, tekst FROM " . $_name . " ORDER BY ID ";
                $_result    = mysqli_query($_connection, $_sql);

                while ($_row = mysqli_fetch_assoc($_result)) {

                    echo "<article class='web_builder_content' draggable='true'>";
                    echo "<input type='text' value='" . $_row['titel'] . "' />";
                    echo "<textarea>" . $_row['tekst'] . "</textarea>";
                    echo "</article>";

                }

                echo "</div>";

                break;

            case "documenten":

                echo "";
                echo "";
                echo "";
                echo "";

                break;

            case "instellingen":

                echo "";
                echo "";
                echo "";
                echo "";

                break;

            default:

                echo "<h1>Deze pagina kon niet gevonden worden</h1>";

        }

    }


    function error () {

    }


?>

JavaScript - &gt; page.js:

function page (page_name) {

    var request = new XMLHttpRequest();

    request.onreadystatechange = function () {
        if (request.readyState == 4 && request.status == 200) {
            document.getElementById("frame-content").innerHTML = request.responseText;
        }
    }

    request.open("GET", "php/page.php?p=" + page_name, true);
    request.send();

}

样式表 - &gt; admin_style.js:

$(document).ready(function () {

    $(body).css('background', 'white');
    $(".total-frame").css('height', ($(window).height() - $(".header").height() - 10)  + 'px');
    $(".mainframe").css('width', ($(window).width() - $(".list-menu").width() - 20 )  + 'px');
    $(".mainframe").css('height', ($(".total-frame").height() - 10 )  + 'px');
    $(".frame-content").css('height', ($(".total-frame").height() - $(".frame-header").height() - 20 )  + 'px');

});

$(window).resize(function () {

    $(".total-frame").css('height', ($(window).height() - $(".header").height() - 10)  + 'px');
    $(".mainframe").css('width', ($(window).width() - $(".list-menu").width() - 20 )  + 'px');
    $(".mainframe").css('height', ($(".total-frame").height() - 10 )  + 'px');
    $(".frame-content").css('height', ($(".total-frame").height() - $(".frame-header").height() - 20 )  + 'px');

});

这是一场彻底的灾难!请帮我!已经,谢谢大家的关注!

0 个答案:

没有答案