0

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;
  }
 ?>
2
  • Start session for getting session variable in any page. so first you need to add session_start(); in top of page after try to get value of session variable. After ajax request successfully need to refresh page. Commented Oct 24, 2015 at 11:57
  • i did that..but what if i want to check the session variable after submit button is clicked and prompt the user with a message that firm name input is valid or not? Commented Oct 24, 2015 at 12:00

1 Answer 1

2

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>.

Sign up to request clarification or add additional context in comments.

2 Comments

i did that..but what if i want to CHECK the session variable after submit button is clicked and prompt the user with a message that firm name input is valid or not?
The php script from address ../registration-control/register-firm.php should start with session_start()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.