-3

php code :

while($row = mysql_fetch_assoc($arr))
    {
        $id = $row['id'];
        $user_from = $row['user_from'];
        $user_to = $row['user_to'];
        $date = $row['date'];
        $msg_body = $row['msg_body'];
        $opened = $row['opened'];
        if(strlen($msg_body)>150)
        {
            $msg_body = substr($msg_body,0,150)."....";
        }
        echo "<div id='toggletext$id' style='background-color:#ADD8E6;padding:6px;' onclick='toggle()'>";
        echo "<form method='post'>";
        echo "<input type='hidden' id='id1' value=$id>";
        echo "<a href='$username'>$username</a>&nbsp";
        echo "&nbsp: $msg_body ";
        echo "</form>";
        echo "</div> <hr/>";
    }

** it will echo the value present in $arr in forms of div so each $arr row will get displayed with unique div id **

js code :

function toggle() 
    {
        var hr = new XMLHttpRequest();
        var id =  document.getElementById("id1").value;
        var tx = "toggletext"+id;
        var fn = document.getElementById(tx);
        var url = "in.php";
        fn.style.backgroundColor = "#fff";
        var v = '<?php echo @$username;?>';
        var vars = "post="+v+"&id="+id;
        hr.open("POST", url, true);
        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        hr.onreadystatechange = function() 
        {
            if(hr.readyState == 4 && hr.status == 200) 
            {
                var return_data = hr.responseText;
                document.getElementById("status").innerHTML = return_data;
            }
        }
        hr.send(vars);
        document.getElementById("status").innerHTML = "processing...";
    }

// The problem is ...when there are 2 div(if $arr has 2 rows)..if i click the second div first div will get selected ..

  • 1
    FYI, [you shouldn't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](https://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which one is best for you. – John Conde Jul 26 '17 at 11:57
  • 2
    Your `id='id1'` is always returning the first id only you need to change that...Try to use `MySqli` – Nawin Jul 26 '17 at 12:00
  • `id`s always need to be unique. `` then for `var id = ` use `this` and find your value. This would be easier with jquery. – chris85 Jul 26 '17 at 12:04
  • Your title really should be more descriptive. – nerdlyist Jul 26 '17 at 12:14

1 Answers1

0

Change your id in your input type hidden

echo "<input type='hidden' id='id".$id."' value=$id>";

and get like this in your javascript...

var incval = '<?php echo $id ?>';
var id =  document.getElementById("id"+incval).value;

I am giving php echo because you already call username.... Finally, try to use My_Sqli function instead my_sql

Nawin
  • 1,653
  • 2
  • 14
  • 23
  • '$username' is global variable ...'$id' is local variable ..we cant echo $id ..now how to extract that 'id$id' in js @Nawin – rahul Jul 26 '17 at 12:26
  • then you can use `$(this)` instead of id and need to declare class name – Nawin Jul 26 '17 at 12:30
  • i am not familiar with jquery ..can u suggest the ans using JavaScript @Nawin – rahul Jul 26 '17 at 12:40
  • @rahul find here https://stackoverflow.com/questions/24796079/toggle-multiple-elements-using-javascript-function-parameters – Nawin Jul 26 '17 at 13:08