2017-07-25 55 views
0
<a href=abc.html name="text1">click me </a> 

插入MySQL表中的超链接源我不得不abc.html发送到一个表在我的数据库,当我点击链接通过单击

+0

有几种方法可以做到这一点。您应该提供更多信息,例如是应该打开的网站,如果是这样的话,如果html实际上包含了php代码,那么这很好吗? – Daidon

回答

0

有达到你正在尝试做不同的方式。 我认为虽然“最干净”将使用jquery(javascript)来操作事件处理程序。

像这样的工作:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<div> 
    <a href="abc.html">click me</a> 
</div> 

<script> 
$(function() { 
    $("a").click(function (e) { 
    var url = $(this).attr("href"); // Here we retrieve the href of clicked link 
    var res = $.ajax({ 
     type: "POST", 
     data: { 
      url: url // The left side is the request key (so: $_POST['url']) 
     }, 
     url: "https://url/to/handler.php", 
     async: false 
    }).responseText; 

    console.log(res); 
    if(res == "success") return true; // returning true lets the browser follow the link 
    else return false; /* so if it didn't work, the link will not be 
          followed. Of course you can do at these two 
          points whatever you like. */ 
    }) 
}); 
</script> 

在PHP的成功简单地echo如果插入成功。

请注意,虽然不赞成使用AJAX请求的async:false。

+0

这确实奏效,非常感谢 –