[英]Post data using ajax when click on a tag
在php文件中,我有這樣的鏈接:
<?php
echo "<a href='example.php add="'.$id.'"'>click</a>"
?>
如果用戶單擊鏈接但沒有取回數據或移至另一頁面,則需要使用Ajax發送變量數據。
以下是操作方法(只需發送POST數據):
var link_data = $('.link').data('data');
$.ajax({
url:'http://yourpage.com/page.php', //This is your url to open when he click the link
type: "POST", //Its the method
data: { link_data : link_data }, //Data to send
success:function (data) {
console.log(data); //Logs the response from the URL to you console.
}
});
這是您的HTML部分:
<?php
echo "<a class='link' href='example.php' data-data='{$id}'>click</a>";
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.