0

I have login form (HTML JS), Im usually create Login Form With PHP, and using Session Variable, but now I Must using only HTML on clien Side and PHP only for server side. Now Im so confused how to read Session Variale inside HTML on Second or Landing Form After Login. this my Code

index.html

<script>
  $(document)
    .ready(function() {
         
      $("#SAVE").on('click', function(){
          
         
           var EMAIL = $("#EMAIL").val();
           var PASS =  $("#PASS").val();
 
             //alert(EMAIL);
             //alert(PASS);
          
           $.post("index-action.php", {email: EMAIL, pass: PASS}, function(datane) {
          
                //$( ".result" ).html( datane );
                if(datane == "sukses"){
                            
                    alert('Login berhasil '); 
                    location.href = '/buku/utama.html';                 
                                        
                }
                else{
                    alert('Input Gagal');
                }///end if 
            });//end post
    
      });//end save
      
     
    })
  ;
  </script>
<body>

<div class="ui middle aligned center aligned grid">
  <div class="column">
    <h2 class="ui teal image header">
      <img src="./login_files/logo.png" class="image">
      <div class="content">
        Log-in Crud Master
      </div>
    </h2>
    <form class="ui large form" id="LOGIN">
      <div class="ui stacked segment">
        <div class="field">
          <div class="ui left icon input">
            <i class="user icon"></i>
            <input type="text" name="email" id="EMAIL" placeholder="E-mail address">
          </div>
        </div>
        <div class="field">
          <div class="ui left icon input">
            <i class="lock icon"></i>
            <input type="password" name="password" id="PASS" placeholder="Password">
          </div>
        </div>
          
        <div class="ui fluid large teal submit button" type="submit" id="SAVE">Login</div>
      </div>

      <div class="ui error message"></div>

    </form>

    <div class="ui message">
      New to us? <a href="https://semantic-ui.com/examples/login.html#">Sign Up</a>
    </div>
  </div>
</div>

</body>

and on Action Form/ server side index-action.php

<?php
session_start();
include "koneksi.php";

   $email    = $_POST['email'];
   $pass     = $_POST['pass'];
  
       $sql = "SELECT * FROM user where email = '$email' and pass = '$pass'";
      
      
    $hasil = mysqli_query($mysqli, $sql);
    $data = mysqli_fetch_assoc($hasil);
    if($data['ID']){
        
        $_SESSION['user'] = $data['user'];
        $_SESSION['hak']  = $data['hak'];
        
    } 
    
    
    if ($hasil) {
        echo "sukses";

    } else {
        echo "input gagalx";

    }   
        
?>

Ok, that Code Work Just Fine and I can goto utama.html after Login, But my Question is, How to get

$_SESSION['user'];

$_SESSION['hak'];

Inside utama.html file. I know that I cant use PHP inside HTML?

Can anyone help to fix This Issue,

Im very appreciate your answer.

Thanks

Community
  • 1
  • 1
Uchsun
  • 361
  • 1
  • 8
  • 25
  • This is not HTML issue, this is rather client / server issue, i.e. client hosted on different web server. For example, you can allow ajax call from client server to you PHP server and call ajax to retrieve session data and then use it in your client JavaScript. Also think about possible security issues you get with this approach. Make sure only your client web server may use session data. – Anatoliy R Oct 30 '19 at 03:40
  • Ok @AnatoliyR How exactly do your answer idea? – Uchsun Oct 30 '19 at 03:49
  • Possible duplicate of [How do I pass variables and data from PHP to JavaScript?](https://stackoverflow.com/questions/23740548/how-do-i-pass-variables-and-data-from-php-to-javascript) – miken32 Oct 30 '19 at 21:38

2 Answers2

0

To read any sort of php variable inside your ja file, you need to send those variable in a json encode format. And after that hold that value in your js file and assign to a cookie. So that you can easily track which user is logged in. Note: You must set cookie in your js.

Let me know if you have any query.

0

OK,

I know The Alternative, I have used localStorage function inside Javascript, so I can use it like Session.

Thanks

Uchsun
  • 361
  • 1
  • 8
  • 25