title: How to pass variable from JavaScript to PHP using jQuery POST
body: <p>I am passing the variable <code>sessionnum</code> from the following Javascript function in the page chat.php:</p>
  
  <pre><code>$(document).ready(function(){
  
          timestamp = 0;
          updateMsg();
          $("form#chatform").submit(function(){
              $.post("backend.php",{
                          message: $("#msg").val(),
                          name: author,
                          action: "postmsg",
                          time: timestamp,
                          tablename1: sessionnum
                      }, function(xml) {
                  $("#msg").empty();
  
                  addMessages(xml);
  
                  document.forms['chatform'].reset()
                  fixScroll();
              });
              return false;
          });
      });
  </code></pre>
  
  <p>To the following PHP function in backend.php:</p>
  
  <pre><code>if(@$action == "postmsg") {
      mysql_query("INSERT INTO `$tablename1` (`user`,`msg`,`time`)
                  VALUES ('$name','$message',".time().")",$dbconn);
      mysql_query("DELETE FROM `$tablename1` WHERE id &lt;= ".
                  (mysql_insert_id($dbconn)-$store_num),$dbconn);
      }
  
  $messages = mysql_query("SELECT user,msg
                           FROM `$tablename1`
                           WHERE time&gt;$time
                           ORDER BY id ASC
                           LIMIT $display_num",$dbconn);
  </code></pre>
  
  <p>It only works when I hard-code an assignment such as <code>$tablename1 = 100</code> in backend.php even though both the variable and its value are integers and the same value.  This hack is not acceptable, as I actually have to pass the variable.  Is there a bug in my code?</p>
  
  <p>This code is adapted from <a href="http://articles.sitepoint.com/article/ajax-jquery/3" rel="nofollow noreferrer">http://articles.sitepoint.com/article/ajax-jquery/3</a> </p>
  
  <p>Thanks for any help POSTING the variable correctly with jQuery.</p>