簡體   English   中英

如何使用 Ajax 通過超鏈接將數據傳遞到 php 頁面

[英]How to pass data through a hyperlink to a php page using Ajax

我正在嘗試使用 Ajax 通過獲取請求將數據從超鏈接傳遞到頁面

最初我可以在沒有 Ajax 的情況下直接執行此操作,如下所示

 <a id="link" href="page.php?id=12&pid=12" >pass data</a>

我在下面的 php

<?php $_GET['id'] ?>
<?php $_GET['pid']?>

現在我想使用 Ajax 來執行此操作,因此不需要加載頁面

我正在嘗試以下

 <a class="choice" id="link" href="page.php?id=12&pid=12" >pass data</a>

$(document).ready(function() {
          
                $(".choice").click(function(e) {
                    e.preventDefault();
                    $.ajax( {
                        <!--insert.php calls the PHP file-->
                        url: "votes.php",
                        method: "get",
                       dataType: 'html',
                        success: function(strMessage) {
                            $("#vote").html(strMessage);
                          
                        }
                    });
                });
            });

但我無法讓它發揮作用。 我需要幫助正確實現使用 Ajax 將數據發送到我的 php 文件

首先,JavaScript 部件需要<script>標簽。 Ajax URL 可以通過$(this).attr('href')<a>標簽的href屬性中讀取。 同樣根據您的代碼,需要一個帶有vote ID 的 div,因此添加了<div id="vote"></div>

<a class="choice" id="link" href="page.php?id=12&pid=12" >pass data</a>
<div id="vote"></div>
<script>
$(document).ready(function() {
    $(".choice").click(function(e) {
        e.preventDefault();
        $.ajax( {
            url: $(this).attr('href'),
            method: "get",
           dataType: 'html',
            success: function(strMessage) {
                $("#vote").html(strMessage);
              
            }
        });
    });
});
</script>
證件通行證數據
<script> $(document).ready(function() { $("#choice").click(function(e) { e.preventDefault(); $.ajax({ url: $(this).attr('href'), method: "GET", dataType: 'html', success: function(strMessage) { $("#vote").html(strMessage); } }); }); }); </script> </body>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM