title: Php: Form auto-fill using $_SESSION variables with multiple php files
body: <p>I already searched for an hour to solve the following problem, but I couldnt find an answer for my specific problem. I have some ideas, which I propose after the introduction to the problem.</p>
  
  <p>Problem:</p>
  
  <p>I have an html form in a php file. You can enter a name (first,mi,last) a phone number and an account number to that form. If a phone number is added, then the onblur function  <em>(using javascript file request.js)</em> on the input field of <em>phone number</em> is used to auto fill the other 4 fields using a hash, which <em>keys</em> are the <em>phone numbers</em> and the <em>values</em> are a json-object, which holds all 4 other values.</p>
  
  <p>If the entered customer data doesn't exist in the hash, then it should be added by clicking on the submit-button using an ajax-request again.</p>
  
  <p>the following shows the file with the input form: <em>index.php</em>:</p>
  
  <pre><code>&lt;?php
     session_start();
  // set the hash to session
  // add the new item to the array/hash
  // reset the array/hash back to session variable
  //
  ?&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;script src="./jquery-2.1.1.min.js"&gt;&lt;/script&gt;
          &lt;script src="./request.js"&gt;&lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;div style="margin-left: 300px"&gt;
              &lt;form id="dataForm" method="POST"&gt;
                  Name: &lt;input type="text" id="fname" name="fname" value="First Name" onfocus="this.value=''"/&gt;
                  &lt;input type="text" id="mi" name="mi" value="Middle Initial"     onfocus="this.value=''"/&gt;
                  &lt;input type="text" id="lname" name="lname" value="Last Name" onfocus="this.value=''"/&gt;&lt;br /&gt;
                  Phone: &lt;input type="text" id="phone" name="phone" value="Phone number" onfocus="this.value=''"/&gt; 
                  &lt;span style="margin-left:113px;"&gt;Account:&lt;/span&gt; &lt;input type="text" id="account" name="account" value="Account number" onfocus="this.value=''"/&gt; &lt;br /&gt;
                  &lt;input style="margin-left:230px" type="submit" name="submit"/&gt;
                  &lt;input type="reset" name="clear"/&gt;
              &lt;/form&gt;
          &lt;/div&gt;
      &lt;/body&gt;    
  &lt;/html&gt;
  </code></pre>
  
  <p>the next file is the javascript file, which handles all actions on the form.
  The getCustomerInformation.php shall include all Session variables, which can be loaded using the ajax calls.</p>
  
  <p>file: <em>request.js</em></p>
  
  <pre><code>$(document).ready(function(){
      $('#phone').blur(function(){
          console.log('on blur was called');
  
          $.ajax({
              url: './getCustomerInformation.php?code=0&amp;phone=' + this.value,            
              success: function(response, status, jqXhr) {
                  var info = JSON.parse(response);
                  $('#fname').val(info.fname);
                  $('#mi').val(info.mi);
                  $('#lname').val(info.lname);
                  $('#account').val(info.account);
                  console.log("successful response");
              },
              error: function(xhr, status, error){
                 alert("Error!" + xhr.status);
              }
          })
      });
  
      $('#dataForm').submit(function(){
          console.log('form submitted');
          var phone = $('#phone').value;
          var fname = $('#fname').value;
          var mi = $('#mi').value;
          var lname = $('#lname').value;
          var account = $('#account').value;
  
          $.ajax({
              url: './getCustomerInformation.php?code=1&amp;phone=' + phone +'&amp;fname='+fname+'&amp;mi='+mi + '&amp;lname='+ lname +'&amp;account='+account,
              success: function(response, status, jqXhr) {
                  console.log("successful SUBMIT response");
              },
              error: function(xhr, status, error){
                 alert("Error!" + xhr.status);
              },
          })
      });
  })
  </code></pre>
  
  <p>file: <em>getCustomerInformation.php</em>:</p>
  
  <pre><code>&lt;?php 
    session_start();
    $_SESSION['1234567'] = '{"lname": "Berg", "mi": "M", "fname": "Thomas", "account": "1234"}';
    $_SESSION['1122334'] = '{"lname": "Jordan", "mi": "C", "fname": "Jacky", "account": "4321"}';
  
    $phone = $_GET['phone'];
    $fname = $_GET['fname'];
    $lname = $_GET['lname'];
    $mi = $_GET['mi'];
    $acc = $_GET['account'];
  
    if($_GET['code'] == 0){
      if(array_key_exists('phone', $_GET) &amp;&amp;  array_key_exists($phone, $_SESSION)){
          print $_SESSION[$phone];
        }
    } else {
      if (array_key_exists('fname', $_GET) &amp;&amp; array_key_exists('lname', $_GET) &amp;&amp; array_key_exists('account', $_GET)){
        $_SESSION[$phone] = '{"lname": "' + $lname+ '", "mi": "'+ $mi + '", "fname": "'+$fname+'", "account": "'+$acc+'"}';
      } else {
        // Data are not complete - not possible to add them to the Session.
      }
    }
  ?&gt;
  </code></pre>
  
  <p>My ideas:</p>
  
  <ol>
  <li><p>I tried to use POST variables instead of GET, but then I wasn't even able to access them in <em>getCustomerInformation.php</em>. I included <em>type: "POST"</em> in the ajax methods, but it didn't help.</p></li>
  <li><p>I thought there might be something wrong with the <em>session_start()</em> call, but I added it to both php files, still no change.</p></li>
  <li><p>First I wanted to use an array as a Session variable and I used it as a hash in order to only need one $_SESSION['hash'] variable which holds all stored values, like:</p>
  
  <p><code>$cust = array("1234567" =&gt; '{"lname": "Berg", "mi": "M", "fname": "Thomas", "account": "1234"}', "1122334" =&gt; '{"lname": "Jordan", "mi": "C", "fname": "Jacky", "account": "4321"}'  );
  $_SESSION['hash'] = $cust;</code></p></li>
  </ol>
  
  <p>Everytime, users enter new data I loaded the array from <em>$_SESSION['hash']</em> and save it back after writing ==> didn't work as well.</p>
  
  <p>Does anyone have an idea, how I can change the code in order to be able to save the data in the backend (<em>getCustomerInformation.php</em>) using $_SESSION variable/s (better with the array-hash)?</p>
  
  <p>Sorry for the large amount of code, I hope it's readable. But I thought, it might be better to include all code</p>
  
  <p>for better understanding.</p>