简体   繁体   English

javascript PHP AJAX刷新DIV的一部分

[英]javascript php AJAX refresh part of a DIV

I was trying to refresh the sidebar class "login" only when a user clicked on the button, however, I am not being able to access the login.php when the user makes the click. 我仅在用户单击按钮时才尝试刷新侧边栏类“登录”,但是,当用户单击按钮时,我无法访问login.php。 When I click on it it's doing nothing and it's refreshing the entire page too. 当我单击它时,它什么也没做,也刷新了整个页面。

For what I could understand AJAX needs to be used to refresh only that DIV and the console.log is not being triggered. 据我所知,仅需使用AJAX刷新DIV,并且不会触发console.log。 What I am doing wrong here? 我在这里做错了什么?

<body>
<div id="main-container">
    <div class="sidebar" id="sidebar">
        <div class="login">

            <?php
            session_start();
            if ( !( isset( $_SESSION['user']) && $_SESSION['user'] != '')) {
                echo '<p>User is logged out</p>';
                echo '<form action="" method="post">';
                echo '<label for="username">Username</label>';
                echo '<input type="text" name="username" id="username_input" required/>';
                echo '<label for="password">Password</label>';
                echo '<input type="text" name="password" id="password_input" required/>';
                echo '<input type="submit" value="Login"  id="login_button">';
                echo '</form>';
                ?>
                <script language='javascript'>
                    $(".login").load( function(event) {
                        event.preventDefault();
                        $("#login_button").click("login.php", function() {
                            console.log("login_button clicked");
                        });
                    })
                </script>
            <?php
            }
            else {
            echo '<p>User is logged in</p>';
            echo '<form action="" method="post">';
            echo '<input type="submit" value="Logout" id="logout_button">';
            echo '</form>';
            ?>
                <script language='javascript'>
                    $(".login").load( function(event) {
                        event.preventDefault();
                        $("#logout_button").click("logout.php", function() {
                            console.log("logout_button clicked");
                        });
                    })
                </script>
            <?php
            }
            ?>

        </div>
        <div class="sideMakers" id="sideMakers">
            <p>Markers</p>
        </div>
    </div>
    <div id="map"></div>
</div>
<script src="map.js"></script>
</body>

Thanks in advance 提前致谢

Your page is refreshing because of action="" on your form tags. 由于表单标签上的action="" ,您的页面令人耳目一新。

  • Also you don't need method="POST" on form tag if you are using AJAX to do so. 另外,如果您使用AJAX,则不需要在表单标签上使用method="POST" Just remove it! 只需将其删除!

  • You may efficiently use ajax request in your scenario. 您可以在方案中有效地使用ajax请求。

    A suggestion: you could separate your js code out of the PHP code. 一个建议:您可以将js代码与PHP代码分开。

so your code will look like this - 因此您的代码将如下所示-

<body>
<div id="main-container">
    <div class="sidebar" id="sidebar">
        <div class="login">
            <?php
                session_start();
                if ( !( isset( $_SESSION['user']) && $_SESSION['user'] != '')) {
                    echo '<p>User is logged out</p>';
                    echo '<form id="loginForm">';
                    echo '<label for="username">Username</label>';
                    echo '<input type="text" name="username" id="username_input" required/>';
                    echo '<label for="password">Password</label>';
                    echo '<input type="text" name="password" id="password_input" required/>';
                    echo '<input type="submit" value="Login"  id="login_button">';
                    echo '</form>';
                } else {
                    echo '<p>User is logged in</p>';
                    echo '<form>';
                    echo '<input type="submit" value="Logout" id="logout_button">';
                    echo '</form>';
                }
            ?>
        </div>
        <div class="sideMakers" id="sideMakers">
            <p>Markers</p>
        </div>
    </div>
    <div id="map"></div>
</div>
<script src="map.js"></script>
<script type="text/javascript">
    $("#login_button").click(function () {
        var data = $("#loginForm").serialize();
        $.ajax({
            type: "POST",
            url: 'login.php',
            data: data,
            success: function (data) {
                console.log(data);
            },
            error: function (error) {
                console.error(error);
            }
        });
        console.log("login_button clicked");
    });
    $("#logout_button").click(function () {
        $.ajax({
            type: "POST",
            url: 'logout.php',
            success: function (data) {
                console.log(data);
            },
            error: function (error) {
                console.error(error);
            }
        });
        console.log("logout_button clicked");
    });
</script>
</body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM