简体   繁体   English

html代码中的php访问会话变量值

[英]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 我想实现的是在html form.Above上打印php session变量。 form.Above是我试图在其中打印valid_firm Session变量的表单,但是用于打印变量的方式不起作用... 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 带有ajax post方法调用的javascript文件以提交表单

  $(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文件来保存会话变量

<?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 我看不到您的session_start()在哪里,您应该在要访问会话的每个页面中首先调用此函数

EDITED 已编辑

What can I say thank you for your -1. 我能说谢谢您的-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 . 当我说您的session_start()在哪里时,我这样做是因为要能够使用php脚本访问会话,必须先调用session_start()才能调用$_SESSION More of that the session_start() should be called before any response that mesans before <!DOCTYPE html> . 更多的session_start()应该在<!DOCTYPE html>之前出现混乱的任何响应之前调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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