0

I'm creating the buttons dynamically based on if the user is logged in or not.

I'm trying to set the onclick function for the loginBtn for example using location="login.php", but that doesn't seem to do it.

I also tried it with:

$("#loginBtn").click(function(){
                $.ajax({
                    type:'POST',
                    url:'login.php'
                    success: function(data) {
                        alert(data);
                    }
                });
            });

but that didn't work either.

Here's the code that creates the buttons:

window.onload = function () {
            var loggedIn = '<?php echo $_SESSION["loggedin"] ?>';
            //Set login buttons to register/login or logout
            if(loggedIn == 1)
            {
                document.getElementById("loginButtons").innerHTML = 
                '<button id="logoutBtn" type="button" class="btn btn-primary myButtons">Abmelden</button>'
            }
            else{
                  document.getElementById("loginButtons").innerHTML = 
                '<button id="regBtn" type="button" class="btn btn-primary myButtons" >Registrieren</button>'+
                '<button id="loginBtn" type="button" class="btn btn-primary myButtons" onclick="location="warungyoga.de/login.php"" >Anmelden</button>'

            }

When the user clicks the Button, it should just lead to the page without any url parameters.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Possible duplicate of [How to manage a redirect request after a jQuery Ajax call](https://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call) – Alvaro Alves Aug 28 '19 at 17:05
  • 1
    Why use Javascript for this? Just put a PHP conditional with a different link depending on if the user is logged in or not. – APAD1 Aug 28 '19 at 17:08
  • @APAD1 Sorry I'm quite new to php, could you give me an example for that? –  Aug 28 '19 at 17:14

3 Answers3

0

You'll need to wait on setting up the click event listener until the document is ready. This is because the element you want to set up with an event listener is created dynamically after page load (rather than being part of the initial html) and isn't immediately ready.

To wait for the document to be ready, wrap that portion of code in:

$( document ).ready(function() { /* code here */ });

Alternatively, you could listen for clicks on the document with a selector for the specific element as the first parameter:

$(document).click("#loginBtn",function(){ ...

Some other lil' bugs:

  • You seem to be missing a comma (See the line url:'login.php').
  • There's no need for a "onclick" attribute when setting up event listeners with jquery.
Web and Flow
  • 144
  • 8
-1

There's no need to use Javascript here, you can just set up a basic PHP conditional statement to display a different link depending on if the user is logged in or not:

<?php
$loggedIn = $_SESSION["loggedin"];

if ($loggedIn == 1) {
  echo '<a id="loginBtn" class="btn btn-primary myButtons" href="/logout.php">Ausloggen</a>';
} else {
  echo '<a id="loginBtn" class="btn btn-primary myButtons" href="/login.php">Anmelden</a>';
}
?>
APAD1
  • 13,509
  • 8
  • 43
  • 72
-1

What are you doing :D

`

// If user logged set session id
if(isUserLogged($email, $pass) > 0){
    // Set user id
    $_SESSION['user']['id'] = 1;
}

if($_SESSION['user']['id'] > 0){
    // user logged
    echo '<a href="/logout.php">LogOut</a>';
}else{
    echo '<a href="/login.php">LogIn</a>';
}
?>
Wicki
  • 1