简体   繁体   中英

php access session variable value in html code

what i would like to achieve is to print php session variable on html form.Above is the form where i am trying to print the valid_firm Session variable but the way it is used to print the variable is not working... newFirm-form.php

<!DOCTYPE>
 <html>
  <head>
    <meta charset="utf-8">
    <meta content="no-cache" http-equiv="cache-control"></meta>
  </head>

<body>
  <form id="firm-form" method="post" />
      <p>Firm Name</p><input class="firm-name" type="text" name="firm-name"  />

      <p>Email</p><input type="text" name="firm-email" />
      <p>Telephone</p><input type="text" name="telephone" />
      <p>Address</p><input type="text" name="address" />
      <p>City</p><input type="text" name="city" />
      <p>Firm Code</p><input type="text" name="firm-code" /> <br/>
  </form>
  <?php print $_SESSION["valid_firm"]; ?>
  <input class="submit" type="submit" value="Submit"><br />
  <span class="error" style="display:none"> Please Fill The Empty Fields</span>
  <span class="success" style="display:none"> Form Submitted Success</span>


</body>
<footer>
  <script type="text/javascript" src="../js/libs/jquery-1.11.3-min.js">     </script>
  <script type="text/javascript" src="../js/newFirm.js"></script>
  </footer>

 </html>

javascript file with ajax post method call to submit the form

  $(document).ready(function() {
     var empty_fields = 0;
     $(".submit").on("click", function(){
       $("#firm-form *").filter(":input").each(function(){
      if(!$(this).val()){
      empty_fields += 1;
    }
  });
  if(empty_fields > 0){
    $(".error").fadeIn(800).show();
    $(".success").fadeOut(800).hide();
  }
  else {
    var firm_name = $(".firm-name").val();
    $.ajax({
      type:"POST",
      url:"../registration-control/register-firm.php",
      data: "firm_name=" + firm_name,
      success: function(){
        $(".success").fadeIn(800).show();
        $(".error").fadeOut(800).hide();
      }
    });
  }
  empty_fields = 0;
    });

  });

and the php file to save the session variable

<?php
if (isset($_POST["firm_name"])){
  validateName($_POST["firm_name"]);

$_SESSION["firm"] = $_POST["firm_name"];
 //echo $_SESSION["firm"]." from register-firm";
  }

   function validateName($firm_name){
       var_dump($firm_name);
      if (preg_match("/^[A-ZΑ-Ω]{1}[a-zA-Zα-ωΑ-Ω0-9\.]*/", $firm_name) &&         !ctype_digit($firm_name)){
     $_SESSION["valid_firm"] = 1;
   }
   $_SESSION["valid_firm"] = 0;
  }
 ?>

I don't see where is your session_start() this function should be first called in every page where you want to access session

EDITED

What can I say thank you for your -1.

When I said where is your session_start() I did it because to be able to access session in a php script you must call session_start() before to call $_SESSION . More of that the session_start() should be called before any response that mesans before <!DOCTYPE html> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM