title: How to stay on same page after login
body: <p>I am building an event registration system which displays event registration list if the user is logged in without page refresh using Ajax. However, when I try to login I get undefined index name on line <code>echo "Hello ".$_SESSION["name"]."&lt;br/&gt;";</code> in index.php. My code is:-<br>
   index.php:-  </p>
  
  <pre><code>&lt;?php
  ob_start();
  session_start();
  require_once('dbconnect.php');
  require_once('function.php');
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Login Registration&lt;/title&gt;
      &lt;link href="style.css" rel="stylesheet"&gt;
      &lt;script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="crossorigin="anonymous"&gt;&lt;/script&gt;
      &lt;script src="script.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div id="wrapper"&gt;
      &lt;!--Login div--&gt;
          &lt;div id="logincontainer"&gt; 
              &lt;form id="loginform" method="post"&gt;
  
                  &lt;h3&gt;Login&lt;/h3&gt;
                  &lt;div class="display-error" style="display: none;"&gt;&lt;/div&gt;
                  &lt;input type="email" name="lemail" placeholder="Enter email address" required&gt;
  
                  &lt;input type="password" name="lpassword" placeholder="Enter password" required&gt;
  
                  &lt;input type="submit" value="Sign In"&gt;
  
                  &lt;p&gt;&lt;a href="forgotpassword.php"&gt;Forgot Password&lt;/a&gt;&lt;/p&gt;
  
                  &lt;p id="bottom"&gt;Don't have an account yet? &lt;a href="#" id="signup"&gt;Sign up&lt;/a&gt;&lt;/p&gt;
  
              &lt;/form&gt;
          &lt;/div&gt;
  
          &lt;div id="signupcontainer"&gt;
              &lt;form id="registerform" method="post"&gt;
                  &lt;h3&gt;Register&lt;/h3&gt;
                  &lt;div class="display-error" style="display: none;"&gt;&lt;/div&gt;
                  &lt;input type="text" name="rname" placeholder="Full Name" required&gt;
  
                  &lt;input type="email" name="remail" placeholder="Enter valid email" required&gt;
  
                  &lt;input type="password" name="rpassword" placeholder="Password" required&gt;
  
                  &lt;input type="text" name="rmobile" maxlength="10" pattern="[0-9]{10}" placeholder="Mobile" required&gt;
  
                  &lt;input type="submit" value="Create Account"&gt;
  
                  &lt;p id="bottom"&gt;Already have an account? &lt;a href="#" id="signin"&gt;Sign In&lt;/a&gt;&lt;/p&gt;
  
              &lt;/form&gt;
  
  
          &lt;/div&gt;
  
          &lt;!--Testing refresh portion--&gt;
          &lt;div id="after-login" style="display: none;"&gt;
              &lt;?php
                  echo "Hello ".$_SESSION["name"]."&lt;br/&gt;";
                  echo '&lt;a href="logout.php"&gt;&lt;span class="glyphicon glyphicon-logout"&gt;&lt;/span&gt;Sign Out&lt;/a&gt;&lt;br/&gt;';
              ?&gt;
              &lt;form id="events" method="post"&gt;
                  Code Ardor&lt;input type="checkbox" name="coding[]" value="ardor"&gt; &amp;nbsp;
                  Designophy&lt;input type="checkbox" name="coding[]" value="design"&gt;&lt;br&gt;
                  &lt;input type="submit" value="Submit" name="submit-btn"&gt;
              &lt;/form&gt;
          &lt;/div&gt;
          &lt;!--Testing portion ends--&gt;
  
      &lt;/div&gt;
  
      &lt;script&gt;
  
          $(document).ready(function(){
              $("#loginform").submit(function(){
                  var data = $("#loginform").serialize();
                  checkRecords(data);
                  return false;
              });
              function checkRecords(data){
  
                  $.ajax({
                      url : 'loginprocess.php',
                      data : data,
                      type : 'POST',
                      dataType : 'json',
                      success: function(data){
                          if(data.code == 200){
                              //alert('You have successfully logged in');
                              //window.location='dashboard.php';
                              $("#logincontainer").hide();
                              $("#after-login").show();
                          }
                          else{
                              $(".display-error").html("&lt;ul&gt;"+data.msg+"&lt;/ul");
                              $(".display-error").css("display","block");
                          }
                      },
                      error: function(){
                          alert("Email/Password is Incorrect");
                  }
                  }); 
              }
          });
  
      &lt;/script&gt;
      &lt;!--Signup Ajax--&gt;
      &lt;script&gt;
  
          $(document).ready(function(){
              $("#registerform").submit(function(){
                  var data = $("#registerform").serialize();
                  signupRecords(data);
                  return false;
              });
              function signupRecords(data){
  
                  $.ajax({
                      url : 'signupprocess.php',
                      data : data,
                      type : 'POST',
                      dataType : 'json',
                      success: function(data){
                          if(data.code == 200){
                              alert('You have successfully Signed Up \n Please Login now.');
                              setTimeout(function(){
                                  location.reload();
                              },500);
                          }
                          else{
                              $(".display-error").html("&lt;ul&gt;"+data.msg+"&lt;/ul");
                              $(".display-error").css("display","block");
                          }
                      },
                      error: function(jqXHR,exception){
                          console.log(jqXHR);
                  }
                  }); 
              }
          });
  
      &lt;/script&gt;
  
  &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p>loginprocess.php</p>
  
  <pre><code>    &lt;?php
  ob_start();
  session_start();
  require_once('dbconnect.php');
  require_once('function.php');
  $errorMsg = "";
  $email = trim($_POST["lemail"]);
  $password = trim($_POST["lpassword"]);
  
  if(empty($email)){
      $errorMsg .= "&lt;li&gt;Email is required&lt;/li&gt;";
  }
  else{
      $email = filterEmail($email);
      if($email == FALSE){
          $errorMsg .= "&lt;li&gt;Invalid Email Format&lt;/li&gt;";
      }
  }
  
  if(empty($password)){
      $errorMsg .= "&lt;li&gt;Password Required.&lt;/li&gt;";
  }
  else{
      $password = $password;
  }
  
  if(empty($errorMsg)){
      $query = $db-&gt;prepare("SELECT password from users WHERE email = ?");
      $query-&gt;execute(array($email));
      $pwd = $query-&gt;fetchColumn();
      if(password_verify($password, $pwd)){
          $_SESSION['email'] = $email;
          //Testing piece
          $qry = $db-&gt;prepare("SELECT name from users WHERE email = ?");
          $qry-&gt;execute(array($email));
          $nme = $qry-&gt;fetchColumn();
          $_SESSION['name']=$nme;
          //Testing code ends
          echo json_encode(['code'=&gt;200, 'email'=&gt;$_SESSION['email']]);
          exit;
      }
      else{
          json_encode(['code'=&gt;400, 'msg'=&gt;'Invalid Email/Password']);
          exit;
      }
  }
  else{
      echo json_encode(['code'=&gt;404, 'msg'=&gt;$errorMsg]);
  }
  
  ?&gt;
  </code></pre>