<html>
<head script type="javascript/text">
<script>
function validate()
{

var html=document.getElementById(<?php '$count1'?>);
<?php
session_start();
$_SESSION['fuserid']=?>="html"

}
</script>
</head>
</html>
<?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);
$count1=0;
while($db_field=mysql_fetch_assoc($result))
{  
     ?>
     <body>
     <a href="displayprofile.php" id=<?php =$count1; ?> onclick="validate(this)">
     <?php  print $db_field['fuserid']."<br>";

     ?>
     </a>
     </body>
     <?php
        $count1=$count1+1; 
     }




mysql_close();
?>

according to $db_field the displayprofile.php will chenge but whenever i am trying to use this code i am getting last $db_field variable ??can any one help to solve this problem.

Dani AI

Generated

As already pointed out, the root problem is mixing server-side PHP and client-side JavaScript as if they run at the same time. PHP runs on the server and emits HTML/JS; the browser can only change server state by making a new request (GET/POST/AJAX). Trying to set a PHP session variable from inline JS without contacting the server will not work, and generating IDs/variables incorrectly in the loop will produce unexpected results (often the last value seen by PHP).

A simple, reliable fix: make each friend link include the friend identifier in the URL so the server knows which profile to show. Example (server-side output):

// server: build safe link text and query string
$safeName = htmlspecialchars($friendName, ENT_QUOTES, 'UTF-8');
$url = 'displayprofile.php?fuserid=' . urlencode($friendName);
echo '<a href="' . $url . '">' . $safeName . '</a><br>';

Then on displayprofile.php read the GET value and, if you need it in session, set it there server-side:

session_start();
if (!empty($_GET['fuserid'])) {
    $_SESSION['fuserid'] = $_GET['fuserid'];
    // lookup and render profile securely (use prepared statements)
}

If you prefer a client-side click handler (so you can read the visible link text), use a data attribute and a small JS redirect. This keeps server logic simple and avoids fragile string-building inside mixed PHP/JS:

echo '<a href="#" data-user="'.htmlspecialchars($friendName).'" onclick="go(this)">'.htmlspecialchars($friendName).'</a>';
function go(el) {
  var name = (el.textContent || el.innerText).trim();
  window.location = 'displayprofile.php?fuserid=' + encodeURIComponent(name);
}

Notes and cautions: always escape output with htmlspecialchars to prevent XSS; use mysqli or PDO with prepared statements (mysql_* is deprecated) to avoid SQL injection; ensure id attributes are unique if used; and don’t expect client-side JS to mutate $_SESSION without a request. These changes will make the loop work predictably and securely for each clicked name.

Recommended Answers

All 3 Replies

You are badly mixing PHP code in a Javascript function.

then how to solve my problem can you plzz provide me the code

<?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];
    ?>

i used the above code and the o/p is :name1
name2
....
then how can i know which name is selected because depending on the selected name my code changes

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.