Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Sir,

I am making a chat project in php so i want to register session variable through jquery, and ajax method

My Code for Registering variable..

JavaScript
var postdata = 'username=' + username + '&password=' + password + '&first=' + first + '&last=' + last + '&mobile=' + mobile + '&email=' + email + '&dob=' + dob + '&gender=' + gender;
        jQuery.ajax({ 
		 type: 'POST',
		 url: 'http://localhost/group/php/register.php',
		 data: postdata,
		 cache: false,
		 success: function(){
				    
					$.post("php/redirecting.php",
					{"username":username});

                    window.location.replace('http://localhost/group/profile.html');
               
            }
        });


php file code redirecting.php

PHP
<?php
session_start();
$uname = $_POST['username'];
$_SESSION['user'] = $uname;
?>


Next page profile.html i want to retrive value from database...

jquery file


$(document).ready(function(){
    getval();
  
});
function getval()
{
    $.ajax({   
        type:"GET",
        url:"http://localhost/group/php/profileedit.php",
        success: function(result)//,var email,var mobile,var dob
        {
          $("#fname").val(result);           
        }
   });    
};


profileedit.php

<pre><?php                    
        if(isset($_SESSION['user'])) 
        {
            $uname = $_SESSION['user'];
        }else
        {
            die('Could not connect: ' . mysql_error()); 
        }    
        $link = mysql_connect('localhost','root','');

        if (! $link) 
        {
            die('Could not connect: ' . mysql_error());
        }

        mysql_select_db("groupchat",$link) or die('couldnt able to connect db');      
        $sql = "select fname,lname from users where username = '$uname'";       
        $result = mysql_query($sql,$link);        
        if(! $result)
        {
            die('Could not get data:'. mysql_error());    
        }        
        $row = mysql_fetch_array($result);
        
        echo trim($row['fname']);    
        echo trim($row['lname']);           
        mysql_close($link);      
  ?> 




but here in the text box it could not retrive value from session variable like its not registering in session but when i give value manually its successfully retrieve value from database and fill in textbox...


please help.... suggest me ...thanks in advance..
Posted
Comments
Archana Palani 27-Oct-14 6:00am    
go with cookies concept.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900