简体   繁体   English

如何在单击超链接时运行php代码

[英]how to run a php code on clicking a hyperlink

I am working on a module, "who viewed your profile" in a social networking site project. 我正在开发一个社交网站项目中的模块“谁查看了您的个人资料”。 When a logged in user clicks on hyperlink of a user, he must be directed to a dummy page called view.Information of user who has clicked link of other user and username of user whose link has been clicked is stored in a database table, views. 登录的用户单击用户的超链接时,必须将其定向到一个名为view的虚拟页面。单击了其他用户链接的用户信息和已单击链接的用户的用户名存储在数据库表中,即views 。 I tried doing this by calling a function updatetable on click of the hyperlink and using this function, I want to send variables viewername and viewedname to view.php, which inserts a record into views table. 我尝试通过单击超链接时调用函数updatetable并使用此函数来执行此操作,我想将变量viewername和viewedname发送给view.php,这会将一条记录插入到views表中。

 while($row=mysqli_fetch_array($res)) { ?> <a href="http://localhost/profileviews/view.php" onclick="updatetable('<?php echo $row['username']; ?>','<?php echo $username; ?>')"><?php echo $row['username']; ?></a> <?php } 

The above code displays links of all users and if a link is clicked, view.php page is opened where some php code has to be run. 上面的代码显示所有用户的链接,如果单击链接,则会打开view.php页面,其中必须运行某些php代码。 My updatetable function is as follows: 我的updatetable函数如下:

 <script type="text/javascript"> var viewedname,viewername; function updatetable(viewedname,viewername) { $.post('view.php' { viewer_name:viewername, viewed_name:viewedname } ); alert(viewedname); } </script> 
view.php code: view.php代码:

 if ($_POST && isset($_POST['viewer_name']) && isset($_POST['viewed_name'])) { $viewer_name = ($_POST['viewer_name']); $viewed_name = mysql_real_escape_string($_POST['viewed_name']); $con=new mysqli('localhost','root','','test'); if($con->connect_error) echo $con->connect_error; $r=mysqli_query($con,"insert into views(viewer,viewed) values('$viewer_name','$viewed_name')"); } 

Problems that I am facing: alert is not being shown and insertion is not happening after view.php code. 我面临的问题:在view.php代码后没有显示警报,也没有发生插入。 Is there a mistake in the way I'm sending the viewer_name and viewed_name variables? 我发送viewer_name和viewed_name变量的方式是否有误? I am new to php. 我是php新手。 Please help me out! 请帮帮我! Thank you :) 谢谢 :)

Missing a ( , ) in your javascript post: 您的JavaScript帖子中缺少( , ):

Replace with: 用。。。来代替:

$.post('view.php', { viewer_name:viewername, viewed_name:viewedname });

PHP: PHP:

Its better doing it this way: 这样更好:

if ($_POST){
  if(isset($_POST['viewer_name']) && isset($_POST['viewed_name'])) {
   //Do your stuff
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM