-3

In my PHP file, I have an echo statement, that outputs some HTML, within which i want to do some assignment based on onclick event.

echo "<td style='padding:10px; text-align:left;'> <a target='_blank' href='stat.php'  onclick='".  $_SESSION['dakno'] = $r[$j]; ."' >".$r[$j]."</a></td>";

I have tried a lot of combinations, but still getting syntax error because of the onclick section.

echo "<td style='padding:10px; text-align:left;'> <a target='_blank' href='stat.php'  onclick='"<?php  $_SESSION['dakno'] = $r[$j]; ?> "' >".$r[$j]."</a></td>";

EDITS:

I am an output field in a table to be a hyperlink. On clicking the link, the value of the clicked item is passed to another PHP file using a SESSION variable.

$sno = 1;
while($r = mysqli_fetch_array($rs)){

                            echo "<tr>";
                            echo "<td style='padding:10px; text-align:left;'>".$sno."</td>"; $sno++;
                            for( $j=1; $j<6; $j++){
                                if($j == 1){
                                echo "<td style='padding:10px; text-align:left;'> <a target='_blank' href='stat.php'  onclick='".  $_SESSION['dakno'] = $r[$j]; ."' >".$r[$j]."</a></td>";


                                continue;
                                }
                                else
                                echo "<td style='padding:10px; text-align:left;'>".$r[$j]."</td>";

                            }
                            echo "</tr>";

                }   

Please, help me to remove the syntax error I am making.

Akash Gupta
  • 308
  • 1
  • 3
  • 15

2 Answers2

1

As specified in the question, I needed to pass the value to another PHP file when someone clicked the link. I did not want to use AJAX here as because I do not expect to update any content dynamically. After two hours of brainstorming, I solved my problem with an extremely basic solution.

$sno = 1;
while($r = mysqli_fetch_array($rs)){
echo "";
                            echo "<tr>";
                            echo "<td style='padding:10px; text-align:left;'>".$sno."</td>"; $sno++;
                            for( $j=1; $j<6; $j++){
                                if($j == 1){

                                echo "<td style='padding:10px; text-align:left;'><form action='stat.php' method='POST'> <input type='hidden' name='dakno' value='".$r[$j]."' > </input> <button class='dakbutton' type='submit'>".$r[$j]."</button></form></td>";


                                continue;
                                }
                                else
                                echo "<td style='padding:10px; text-align:left;'>".$r[$j]."</td>";

                            }
                            echo "</tr>";
                            echo "</form>";
                }   
Akash Gupta
  • 308
  • 1
  • 3
  • 15
-1
echo "<td style='padding:10px; text-align:left;'> <a target='_blank' href='stat.php'  onclick='" .
$_SESSION['dakno'] = $r[$j] .
    "' >" .
    $r[$j].
    "</a></td>";

remove ; in $_SESSION['dakno'] = $r[$j];

Larry.Z
  • 3,694
  • 1
  • 20
  • 17
  • on removing the ';', it simply runs likes normal PHP and doesn't execute on click event. Basically it executes every time without knowing if the event occurred or not. – Akash Gupta Aug 24 '13 at 08:23
  • I just want to solve the **syntax error** problem. I suggest to use AJAX to pass value to other php file – Larry.Z Aug 24 '13 at 08:29
  • 1
    @Larry.Z Didn't downvote, but you should point it out in the comments. That's not what the answer field is for. It's for answering the question, fixing any errors present is an aside. – Daedalus Aug 24 '13 at 08:31
  • @Daedalus, Thanks, I will think about it next time :) – Larry.Z Aug 24 '13 at 08:35