#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Posts
    213
    Rep Power
    16

    How to pass a php session variable


    Hello.

    I have a webpage with a javascript that passes variables to a php script to be put in a mysql database. I'm trying to add a php session variable to the php script, but it's not being passed into the database. Should I do it in the javascript instead? Here are the scripts:

    Javascript:
    Code:
    <script type="text/javascript">
    function image() {
    	var cmd = 'IMAGE';
    	var zoom = 'NULL';
    	var movement = 'NULL';
    	var presets = 'NULL';
    
    	$.post('mupdate.php', {
    			cmd: cmd
    			,zoom: zoom
    			,movement: movement
    			,presets: presets
    	});
    };
    </script>
    php script:
    Code:
    <?php
    
    $cmd = $_POST['cmd'];
    $zoom = $_POST['zoom'];
    $movement = $_POST['movement'];
    $presets = $_POST['presets'];
    $user = $_POST['$_SESSION['username']'];
    
    //Connect and select database
    	mysql_connect("host", "username", "password") or die(mysql_error());
    	mysql_select_db("tutorial") or die(mysql_error());
    
    	$query="INSERT INTO example (cmd, zoom, movement, presets, user) VALUES ('".$cmd."', '".$zoom."', '".$movement."', '".$presets."', '".$user."')";
    
    	mysql_query($query) or die(mysql_error());
    
    ?>
    I've also tried doing this with the php script:
    Code:
    <?php
    
    $cmd = $_POST['cmd'];
    $zoom = $_POST['zoom'];
    $movement = $_POST['movement'];
    $presets = $_POST['presets'];
    
    //Connect and select database
    	mysql_connect("host", "username", "password") or die(mysql_error());
    	mysql_select_db("tutorial") or die(mysql_error());
    
    	$query="INSERT INTO example (cmd, zoom, movement, presets, user) VALUES ('".$cmd."', '".$zoom."', '".$movement."', '".$presets."', '".'$_SESSION['username'."')";
    
    	mysql_query($query) or die(mysql_error());
    
    ?>
    I am able to use the php session variable in my other php pages on the site. I really wasn't sure where to post this, if I should post elsewhere just let me know. Thanks for any help.
    --
    Moderation is the key to happiness and harmony in one's life.
  2. #2
  3. No Profile Picture
    I AM A GOLDEN GOD
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Apr 2003
    Location
    Camarillo, California
    Posts
    5,929
    Rep Power
    1171
    Unless you haven't posted all of your code, you never start the session, so you don't have any access to the session data. Use [phpnet=session_start]session_start()[/phpnet] as the first line of your script.

    Now, having said that, how would a session value be inside a POST variable? The POST data is whatever you've POSTed to the script. Your second example is better, but you still need to start the session first (and of course have had to assign a value to the session variable).

    Comments on this post

    • tinyang agrees
    "Seriously, we're not a search engine, we're actual people." ~ ManiacDan

    BookMooch.com : Give books away. Get books you want.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Posts
    213
    Rep Power
    16
    That was it, I forgot to start the session in that script! Thank you very much!
    --
    Moderation is the key to happiness and harmony in one's life.

IMN logo majestic logo threadwatch logo seochat tools logo