<?php
session_start();
$database="myfriend";
$userid=$_SESSION['user'];
mysql_connect("127.0.0.1","root","");
$db_found=mysql_select_db($database);
$sql="select fuserid from friends where userid='$userid';";
$result=mysql_query($sql);
$id=0;
$users=array();
?>
<body >

<center>
<p> your friends list:</p>

<?php
while($db_field=mysql_fetch_assoc($result))
{  
      ?>

      <a href="displayprofile.php" id='$id' onclick="validate(this)"><?php print $db_field['fuserid']."<br><br>";?></a>
      <?php
          $users[$id]=$db_field['fuserid'];
          $id=$id+1;
 }?>

<head script type="javascript/text">
<script>
function validate()
{
    var id1=document.getElementById(</script><?php print $id ?><script>);
    </script>

<?php
$_SESSION['id'] ?><script >=id1;
}
    </script>    

    <?php
    $id1=$_SESSION['id'];
    $_SESSION['fuserid']=$users[$id1];
    ?>

thw o/p:name1
name2
..
depending on the selected name my code will be changing .Then how to know which item of while loop is selected at run time.plzz some one help this code is used in my project that displays profile of particular user depending on name.

you have several problems with your code
1 in HTML: <head> goos before <body>

2 the <head> tag does not have a script atrbute

<head>
<script type="javascript/text">

3 don't close the javascipt tag to insert php

var id1=document.getElementById(<?php print $id ?>);

4 ... well fix 1b to 3 first

i changed the code to

<head>
<script type="javascript/text">
function validate()
{
var id1=document.getElementById(<?php print $id ?>);
<?php
    $_SESSION['id1']?>=id1;
<?php
    $id1=$_SESSION['id1'];
$_SESSION['fuserid']=$array[$id1];
?>
}
</script>
</head>
<?php
session_start();
$database="myfriend";
$userid=$_SESSION['user'];
mysql_connect("127.0.0.1","root","");
$db_found=mysql_select_db($database);
$sql="select fuserid from friends where userid='$userid';";
$result=mysql_query($sql);
$id=0;
$users=array();
?>
<body >

<center>
<p> your friends list:</p>

<?php
while($db_field=mysql_fetch_assoc($result))
{  
     ?><a href="displayprofile.php" onclick="validate()"><?php print $db_field['fuserid']."<br><br>";?></a>
     <?php
$id=$id+1;
$users=$db_field['fuserid'];
 }


 ?>

but the id1 value is saved to last id value of <a> tag but not the clicked item id value .So,can you plzz provide me code to get selected id value plzzzzzzzzz

everthing between <?php and ?> is executed by the webserver.
the result is sent to the brouwer.
It is the brouwser that executes the javascript function
the php variable $_SESSION is meaningless there.

what you nead is AJAX or JQUERY

on 2e thought:
it better to not use javascript for this at all
let displayprofile.php do the session stuf
you nead to pass the userid to displayprofile.php

while($db_field=mysql_fetch_assoc($result))
{
?><a href="displayprofile.php?userid=<? print $db_field['fuserid'] ?>" onclick="validate()"><?php print $db_field['fuserid']."<br><br>";?></a>

with out using onclick function then how can we know which item is selected??and displayprofile.php?userid=<? print $db_field ?> is not working means with this code not even eble to get previous o/p like list of names.If you do't mind can you plzz provide me the code clearly this is too important in my project

You have to edit displayprofile.php

the userid will be in:

$_GET['userid']

since you don't use the onclick anymore, remove it

while($db_field=mysql_fetch_assoc($result))
    {
    ?><a href="displayprofile.php?userid=<? print $db_field['fuserid'] ?>" >
    <?php print $db_field['fuserid']."<br><br>";?></a>

hmm this is working thaq to all who replied to my post

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.