Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8010525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:45:39+00:00 2026-06-04T18:45:39+00:00

I have a php-file that i want to put to work in a div:

  • 0

I have a php-file that i want to put to work in a div:

<?php session_start(); ?>
<?php function loginForm() { ?>
<form method="POST" action="" name="loginForm" id="loginForm" accept-charset='UTF-8'>
    <input type='hidden' name='submit' id='submit' value='1'/>
    User: <input type="text" name="user" id="user" /><br />
    Password: <input type="password" name="passwd" id="passwd" /><br />
    <input type="submit" name="login" id="login" value="Login" />
</form>
<?php } ?>

<?php function logoutForm() { ?>
    <form method="POST" action="" name="logoutForm" id="logoutForm">
        Logout: <input type="submit" value="Log out" name="logout" id="logout" />
    </form>
<?php } ?>

<?php
function loggedUser() {
    if(isset($_SESSION['user']) && isset($_SESSION['passwd'])) {
        return $_SESSION;
    } else {
        return array();
    }
}

function loginLogout() {
    $loggedUser = loggedUser();
    if(!empty($loggedUser)) {
        logoutForm();
    } else {
        loginForm();
    }
    if(isset($_POST['login']) && isset($_POST['user']) && isset($_POST['passwd']) &&
            $_POST['user'] == "admin" && $_POST['passwd'] == "admin") {
        $_SESSION['user'] = $_POST['user'];
        $_SESSION['passwd'] = $_POST['passwd'];
        header('Refresh: 1; url='.$_SERVER['REQUEST_URI']);
    }
    if(isset($_POST['logout']) && isset($_SESSION)) {
        foreach($_SESSION as $k => $v) {
            unset($_SESSION[$k]);
        }
        header('Refresh: 1; url='.$_SERVER['REQUEST_URI']);
    }
}
loginLogout();

?>

And also i have codes in jquery and html

<script type="text/javascript">
    $(document).ready(function(){
        $('#logindiv').load('loginLogout.php');
        $('#loginForm, #logoutForm').live('submit', function(e) {
            e.preventDefault();
            $.post('loginLogout.php', $(this).serialize(), function (
                    data, textStatus) {
                $('#logindiv').html(data);
            });
            return false;
        });
    });
</script>

and the div

<div id="logindiv"></div>

How to put correctly the code, that log in and log out works also in jquery without refreshing the whole page? What is a refresh event that recognize a line header(‘Refresh: 1; url=’.$_SERVER[‘REQUEST_URI’]); in the php-file. Widhout div and jquery it works well.

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:45:41+00:00Added an answer on June 4, 2026 at 6:45 pm

    Here is a full working example using 2 files that will allow the login/logout form to update dynamically without a page refresh. The code is very similar to yours.

    Code for form.php

    <?php 
    session_start(); 
    
    function loginForm($isHidden) { 
        $display = ($isHidden) ? "none" : "block";
        ?>
        <form method="POST" action="" name="loginForm" id="loginForm" accept-charset='UTF-8' style="display: <?php echo $display; ?>;">
            <input type='hidden' name='actionLogin' id='actionLogin' value='1'/>
            User: <input type="text" name="user" id="user" /><br />
            Password: <input type="password" name="passwd" id="passwd" /><br />
            <input type="submit" name="login" id="login" value="Login" />
        </form>
        <?php 
    } 
    
    function logoutForm($isHidden) { 
        $display = ($isHidden) ? "none" : "block";
        ?>
        <form method="POST" action="" name="logoutForm" id="logoutForm" style="display: <?php echo $display; ?>;">
            <input type='hidden' name='actionLogout' id='actionLogout' value='1'/>
            Logout: <input type="submit" value="Log out" name="logout" id="logout" />
        </form>
        <?php 
    }
    
    function loggedUser() {
        if(isset($_SESSION['user']) && isset($_SESSION['passwd'])) {
            return $_SESSION;
        } else {
            return array();
        }
    }
    ?>
    
    <html>
    <head>
        <title>Form</title>
    
        <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
    
        <script type="text/javascript">
            $(document).ready(function(){
                $('#loginForm, #logoutForm').live('submit', function(e) {
                    e.preventDefault();
                    $.post('loginLogout.php', $(this).serialize(), function (data, textStatus) {
                        if (data == "true") { //login successful
                            $("#loginForm").hide();
                            $("#logoutForm").show();
                        } else {
                            $("#loginForm").show();
                            $("#logoutForm").hide();
                        }
                    });
                    return false;
                });
            });
        </script>
    </head>
    <body>
    
        <div id="logindiv">
            <?php
            $loggedUser = loggedUser();
            if(!empty($loggedUser)) {
                loginForm(true); //hidden
                logoutForm(false); //not hidden
            } else {
                loginForm(false);
                logoutForm(true);
            }
            ?>
        </div>
    
    </body>
    </html>
    

    Code for loginLogout.php

    <?php
    session_start(); 
    
    if(isset($_POST['actionLogin']) && isset($_POST['user']) && isset($_POST['passwd']) &&
            $_POST['user'] == "admin" && $_POST['passwd'] == "admin") {
        $_SESSION['user'] = $_POST['user'];
        $_SESSION['passwd'] = $_POST['passwd'];
        echo "true"; //successful log in
    
    } else if(isset($_POST['actionLogout']) && isset($_SESSION)) {
        foreach($_SESSION as $k => $v) {
            unset($_SESSION[$k]);
        }
        echo "false"; //successful log out
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php file that returns a div and a javascript. I want
I have a file with .php extention www.example.com/thefile.php?name=123 that I want to direct the
I have a php file that contains a form (which contains 2 input boxes
I have a php file that looks like this: <?php include(config.php); // put the
I have a PHP page with a form. The form has method=POST and the
I have a php file that contains an array $myArray . <?php $myArray =
I have a php file that includes two functions, one to connect to the
I have a PHP file that create an array of class with array_push and
I have a PHP file that is ran every so often to delete old
I have a PHP file that contains a lot of if and else statements

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.