Snippet menu using jQuery and PHP I have been trying to manage users using jQuery &amp; PHP instead using just PHP because I think it is more comfortable to use jQuery &amp; PHP but using just PHP is easier at my personal point though more organized.

Since I'm not an expert using jQuery I would like to know if the way I'm managing my users is optimal or should I return to full PHP?

Let's take an example of how I work with jQuery+PHP.

I have 3 kind of users:


Guests
Users
Admins


I will start with my Login.php which code I will skip because I think is irrelevant. Login.php will start my sessions.

Session_handler.php

This helps me to focus which type of users is logged at the moment:

session_start();
if(isset($_SESSION['Usuario']))
{
    if( $_SESSION['estado'] == 'User'){
    $send = array('ID' =&gt; $_SESSION['ID'], 'user' =&gt; $_SESSION['Usuario'], 'estado' =&gt; 'usuario' ); 
    echo json_encode($send); 
    }
    if( $_SESSION['estado'] == 'Admin'){
    $send = array('ID' =&gt; $_SESSION['ID'], 'user' =&gt; $_SESSION['Usuario'], 'estado' =&gt; 'admin' ); 
    echo json_encode($send); 
    }

}
else{
    $send = array('estado' =&gt; 'invitado' ); 
    echo json_encode($send);
}


Sessions.js

Now I will manage my users menu depending on which type of user is logged in:

$(document).ready(function(){
    $.ajax({
        url:'js/Session_handler.php', 
        success:function(data) { 
            var result = $.parseJSON(data);
            var i = 0;
            var carrito = '&lt;li&gt;&lt;a href="Carrito.html"&gt;&lt;span class="glyphicon glyphicon-shopping-cart" aria-hidden="true"&gt;&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;';
            var admin = '&lt;li&gt;&lt;a href="Cuenta_admin.html"&gt;[Admin] '+result.user+'&lt;/a&gt;&lt;/li&gt;';
            var cuenta = '&lt;li&gt;&lt;a href="Cuenta_us.html"&gt;'+result.user+'&lt;/a&gt;&lt;/li&gt;';
            var navegacion = '&lt;li&gt;&lt;a href="Navegacion/Entrar.html"&gt;Entrar&lt;/a&gt;&lt;/li&gt;';
            var registrase = '&lt;li&gt;&lt;a href="Navegacion/Registrarse.html"&gt;Registrarse&lt;/a&gt;&lt;/li&gt;';
            var salir = '&lt;li&gt;&lt;a href="Navegacion/Php/logout.php"&gt;Salir&lt;/a&gt;&lt;/li&gt;';
            if(result.estado == 'invitado'){
                $('#snipuser').append(navegacion,registrase);  
            }
            if(result.estado == 'usuario'){
                $('#snipuser').append(carrito,cuenta,salir);
            }
            if(result.estado == 'admin'){
                $('#snipuser').append(carrito,admin,salir);
            }
        }
    });
});


Then I just need to call my id to show the menu depending on which user is logged in:

Index.html

&lt;div id="snipuser"&gt;&lt;/div&gt;


OK, now I will ask again in case my doubt was not clear.

Is it more effective to work this way (JQuery+PHP) than working with full PHP? What does it mean to work with full PHP? I will show an example.

Login.php that will start my sessions.

Now instead working with Index.html I will be bound change my file extension to Index.php and work my menus like this:

&lt;?php
  session_start(); 
   if(isset($_SESSION['Usuario']) and $_SESSION['estado'] == 'Autenticado')                     { ?&gt;
    &lt;a href="Navegacion/Cuenta.php"&gt; &lt;?php echo $_SESSION['Usuario'];?&gt; &lt;/a&gt;
    &lt;a href="Navegacion/Carrito.php"&gt;&lt;img src="Images/Carrito.png" alt=""  width="20" height="20" &gt;&lt;/a&gt;
    &lt;a href="Navegacion/Php/Logout.php"&gt;Salir&lt;/a&gt;
 &lt;?php }    
    else {    ?&gt;
        &lt;a href="Navegacion/Entrar.php"&gt;Entrar&lt;/a&gt;
        &lt;a href="Navegacion/Registrarse.php"&gt;Registrarse&lt;/a&gt;
&lt;?php   }    ?&gt;


jQuery+PHP or full PHP?
Reasons not to substitute Javascript for PHP

If you use Javascript (including jQuery), then your site won't work if Javascript is disabled.  

To use Javascript, you have to give unauthenticated users the same information as authenticated users and even admins.  Someone can save the HTML and edit it to show the full information.  This doesn't matter much with a menu, but with data...  

Formatting


&lt;?php
  session_start(); 
   if(isset($_SESSION['Usuario']) and $_SESSION['estado'] == 'Autenticado')                     { ?&gt;
    &lt;a href="Navegacion/Cuenta.php"&gt; &lt;?php echo $_SESSION['Usuario'];?&gt; &lt;/a&gt;
    &lt;a href="Navegacion/Carrito.php"&gt;&lt;img src="Images/Carrito.png" alt=""  width="20" height="20" &gt;&lt;/a&gt;
    &lt;a href="Navegacion/Php/Logout.php"&gt;Salir&lt;/a&gt;
 &lt;?php }    
    else {    ?&gt;
        &lt;a href="Navegacion/Entrar.php"&gt;Entrar&lt;/a&gt;
        &lt;a href="Navegacion/Registrarse.php"&gt;Registrarse&lt;/a&gt;
&lt;?php   }    ?&gt;



I find your use of beginning and ending tags confusing.  Consider 

&lt;?php
  session_start(); 
  if (isset($_SESSION['Usuario']) &amp;&amp; $_SESSION['estado'] == 'Autenticado') {
?&gt;
    &lt;a href="Navegacion/Cuenta.php"&gt; &lt;?php echo $_SESSION['Usuario']; ?&gt; &lt;/a&gt;
    &lt;a href="Navegacion/Carrito.php"&gt;&lt;img src="Images/Carrito.png" alt=""  width="20" height="20" &gt;&lt;/a&gt;
    &lt;a href="Navegacion/Php/Logout.php"&gt;Salir&lt;/a&gt;
&lt;?php
  }    
  else {
?&gt;
        &lt;a href="Navegacion/Entrar.php"&gt;Entrar&lt;/a&gt;
        &lt;a href="Navegacion/Registrarse.php"&gt;Registrarse&lt;/a&gt;
&lt;?php
  }
?&gt;


By always putting tags at the left margin, it is easier to see where code blocks begin and end.  Only the embedded output has the tags inline as well.  This will look more familiar to most people who might edit your code later.