title: Reading a session variable to write to the database
body: <p>If someone could help me out with this I'd really appreciate it - I almost have it working!</p>
  
  <p>Basically, I'm making my own application idea for a while, the reason I'm doing it is because I am new to coding, so I felt it would teach me a lot about the different elements (i.e. adding, updating and deleting records).</p>
  
  <p>So with this example, I have a user logging in successfully and it's creating a session which is being passed along the other forms. I want them to be able to add a deal - which I almost have but seem to struggle with one part. </p>
  
  <p>My HTML code is:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Find A Deal&lt;/title&gt;
  &lt;meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0"/&gt;
  &lt;link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" /&gt;
  &lt;link rel="stylesheet" href="http://localhost/findadeal/themes/deal.css" /&gt;
  &lt;style&gt;
      #login-button {
          margin-top: 30px;
      }        
  &lt;/style&gt;
  &lt;script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js"&gt;&lt;/script&gt;    
  &lt;script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"&gt;&lt;/script&gt;
  &lt;script src="js/custom3.js"&gt;&lt;/script&gt;
  &lt;script src="js/custom4.js"&gt;&lt;/script&gt;
  
         &lt;?php
      if( !isset( $_SESSION ) ){
       session_start();
          }
  
      if( isset( $_SESSION['username'] ) ){
        /* User is logged in */
        echo "IT WORKS!";
          } ?&gt;
  
  &lt;/head&gt;
  
  
  &lt;body&gt;
  &lt;div data-role="page" id="login"&gt;
      &lt;div data-theme="a" data-role="header"&gt;
          &lt;h3&gt;Find A Deal&lt;/h3&gt;
      &lt;/div&gt;
  
      &lt;div data-role="content"&gt;
  
          &lt;label for="username"&gt;Enter your username:&lt;/label&gt;
          &lt;input type="text" value="" name="username" id="username"/&gt;
          &lt;label for="password"&gt;Enter your password:&lt;/label&gt;
          &lt;input type="password" value="" name="password" id="password"/&gt;  
          &lt;a data-role="button" id="login-button" data-theme="b"&gt;Login&lt;/a&gt;
      &lt;/div&gt;
  
      &lt;div data-theme="a" data-role="footer" data-position="fixed"&gt;
  
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;!--Newly rendered page after successful login!--&gt;
  
  &lt;div data-role="page" id="index"&gt;
      &lt;div data-theme="a" data-role="header"&gt;
          &lt;h2&gt;Find A Deal&lt;/h2&gt;
      &lt;/div&gt;
  
      &lt;div data-role="content"&gt;
  
             &lt;?php
      if( !isset( $_SESSION ) ){
       session_start();
          }
  
      if( isset( $_SESSION['username'] ) ){
        echo "IT WORKS!";
          } ?&gt;
  
  
      &lt;h3&gt;&lt;/h3&gt;
      &lt;a href="#view" data-role="button" data-icon="search"&gt;View Deals&lt;/a&gt;
      &lt;a href="#add" data-role="button" data-icon="plus"&gt;Add Deals&lt;/a&gt;
  
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;!--View Deal Page--&gt;
  
      &lt;div data-role="page" id="view"&gt;
      &lt;div data-role="header"&gt;
          &lt;h2&gt;Find A Deal&lt;/h2&gt;
      &lt;/div&gt;
  
      &lt;div data-role="content"&gt;
  
             &lt;?php
      if( !isset( $_SESSION ) ){
       session_start();
          }
  
      if( isset( $_SESSION['username'] ) ){
        echo "IT WORKS!";
          } ?&gt;
  
  
      &lt;h3&gt;&lt;/h3&gt;
      &lt;p&gt;test so I know I'm onto a new page&lt;/p&gt;
      &lt;a href="#view" data-role="button" data-icon="search"&gt;View Deals&lt;/a&gt;
      &lt;a href="#add" data-role="button" data-icon="plus"&gt;Add Deals&lt;/a&gt;
      &lt;/div&gt;
  
  &lt;div data-role="footer"&gt;
  
  &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;!--Add Deal Page--&gt;
  
      &lt;div data-role="page" id="add"&gt;
      &lt;script src="js/custom4.js"&gt;&lt;/script&gt;
      &lt;div data-role="header"&gt;
          &lt;h2&gt;Find A Deal&lt;/h2&gt;
      &lt;/div&gt;
  
      &lt;div data-role="content"&gt;
  
             &lt;?php
      if( !isset( $_SESSION ) ){
       session_start();
          }
  
      if( isset( $_SESSION['username'] ) ){
          echo "It's working!";
          } ?&gt;
  
      &lt;label for="name"&gt;Deal Name:&lt;/label&gt;
          &lt;input type="text" value="" name="name" id="name"/&gt;
          &lt;label for="desc"&gt;Description&lt;/label&gt;
          &lt;input type="text" value="" name="desc" id="desc"/&gt;  
          &lt;a data-role="button" id="submit-button" data-theme="b"&gt;Submit&lt;/a&gt;
      &lt;/div&gt;
  
  
  
  
  
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>So just to explain that - the first page is a login page, it brings you next to index which is just a menu page I've set up, then from the menu you can select 1 of 2 pages - view deal and add deal. I'm working with the add deal one at the minute.</p>
  
  <p>When adding a deal the user inputs a deal name and a description into the relevant text boxes so it can be added over to the database. 
  I am using this JS function:</p>
  
  <pre><code>//Adding a new deal
  
  $(document).on('pagebeforeshow', '#add', function(){ 
   $('#submit-button').on('click', function(){
      if($('#name').val().length &gt; 0 &amp;&amp; $('#desc').val().length &gt; 0){
          userObject.name = $('#name').val(); // Put username into the object
          userObject.desc = $('#desc').val(); // Put password into the object
          // Convert an userObject to a JSON string representation
          var outputJSON = JSON.stringify(userObject);
          // Send data to server through ajax call
          // action is functionality we want to call and outputJSON is our data
          ajax.sendRequest({action : 'add', outputJSON : outputJSON});
      } else {
          alert('Please fill all nececery fields');
      }
   });    
  });
  
  $(document).on('pagebeforeshow', '#index', function(){ 
  if(userObject.name.length == 0){ // If username is not set (lets say after force page refresh) get us back to the login page
      $.mobile.changePage( "#add", { transition: "slide"} ); // In case result is true change page to Index  
  }
  $(this).find('[data-role="content"] h3').append('Deal Added:' + userObject.name); // Change header with added message
  //$("#index").trigger('pagecreate');
  });
  
  // This will be an ajax function set
  var ajax = {
  sendRequest:function(save_data){
      $.ajax({url: 'http://localhost/findadeal/login/json3.php',
          data: save_data,
          async: true,
          beforeSend: function() {
              // This callback function will trigger before data is sent
              $.mobile.showPageLoadingMsg(true); // This will show ajax spinner
          },
          complete: function() {
              // This callback function will trigger on data sent/received complete
              $.mobile.hidePageLoadingMsg(); // This will hide ajax spinner
          },
          success: function (result) {
              if(result == "true") {
                  $.mobile.changePage( "#index", { transition: "slide"} ); // In case result is true change page to Index
              } else {
                  alert('Deal Addition has been unsuccessful, please try again!'); // In case result is false throw an error
              }
              // This callback function will trigger on successful action
          },
          error: function (request,error) {
              // This callback function will trigger on unsuccessful action                
              alert('Error has occurred, please try again!');
          }
      });
  }
  }
  
  // We will use this object to store username and password before we serialize it and send to server. This part can be done in numerous ways but I like this approach because it is simple
  var userObject = {
  name : "",
  desc : ""
  }
  </code></pre>
  
  <p>This will then be fed over to a PHP file, ready to write the input to the database like this:</p>
  
  <pre><code>&lt;?php
  
  $var1 = $_REQUEST['action']; // We dont need action for this tutorial, but in a complex code you need a way to determine ajax action nature
  $jsonObject = json_decode($_REQUEST['utputJSON']); // Decode JSON object into readable PHP object
  
  $name = $jsonObject-&gt;{'name'}; // Get name from object
  $desc = $jsonObject-&gt;{'desc'}; // Get desc from object
  
  mysql_connect("localhost","root","");  // Conect to mysql, first parameter is location, second is mysql username and a third one is a mysql password
  @mysql_select_db("findadeal") or die( "Unable to select database"); // Connect to database called test
  
  $query = "INSERT INTO deal (dname, description) VALUES ('$name' ,'$desc')";
  $result=mysql_query($query);
  $num = mysql_numrows($result);
  
  if($num != 0) {
  
      echo "true";
  
  } else {
  
      echo "false"; 
  
  }
  ?&gt;
  </code></pre>
  
  <p>This is all working fine, however, the reason the data is not being inserted to the database is because it needs an ID from the user of which the username is being stored in the session variable.</p>
  
  <p>What I would like help with is:</p>
  
  <p><strong>1.</strong> Can someone show me how you get the data from the session variable and have it brought over to the PHP file.
  <strong>2.</strong> Also, the session variable is storing the username - is there a way I can go about checking for its relevant userid tag on its own or will this have to be done through the SQL statement in the PHP file.</p>
  
  <p>I've hope I've not caused any confusion and I've made myself clear about what it is I'm having trouble with. I am a student so all help would be appreciated in the learning process! I'm getting confused in thinking should the session data go though the javascript function like the other data or should it be able to read straight over to the writing PHP file or what? =/ </p>
  
  <p>Thanks for your time! </p>