簡體   English   中英

使用ajax將值從一頁傳遞到另一頁

[英]pass value from one page to another using ajax

我希望通過ajax將數據從另一個頁面調用到現有頁面。 為此,我有以下代碼

<script>
$(document).ready(function()
    {
        $(".link").on("click", function(e)
            {
                e.preventDefault();
                var id = $(this).data("id");
                console.log (id); // i am getting value in this id 
                $.ajax({
                type : "post",
                url : "register.php",
                data :  id,
                cache : false,
                success : function(html)
                {
                    $('#msg').html(html);
                }});
  });
});
</script>

<a class='link' data-id="$idd" >TEXT</a>

直到console.log(id)代碼正常工作,我在id內獲取了價值,但我無法運行register.php頁面。 我希望攜帶id到register.php頁面,在那里運行一些代碼並在#msg下打印其結果,任何人都可以告訴我如何糾正我的Ajax代碼

您需要發送類似data的數據:{id:id}

<script>
$(document).ready(function()
    {
        $(".link").on("click", function(e)
            {
                e.preventDefault();
                var id = $(this).data("id");
                console.log (id); // i am getting value in this id 
                $.ajax({
                type : "post",
                url : "register.php",
                data :  {id: id},
                cache : false,
                success : function(html)
                { alert(html);
                    $('#msg').html(html);
                }});
  });
});
</script>

希望這能解決您的問題。

您應該以key/value格式傳遞數據。 現在,您可以通過打印POST數組來檢查register.php的數據。

您可以像示例中所示的那樣以純字符串形式傳遞數據,也可以傳遞JSON對象。

  • “鍵1 =值&鍵2 =值...。”
  • {k1:v1,k2:v2,......}

     <script> $(document).ready(function() { $(".link").on("click", function(e) { e.preventDefault(); var id = $(this).data("id"); console.log (id); // i am getting value in this id $.ajax({ type : "post", url : "register.php", data : "id="+id, //you can pass value like this. cache : false, success : function(html) { $('#msg').html(html); } }); }); }); </script> 

檢查傳遞的值方法

<script>
$(document).ready(function()
    {
        $(".link").on("click", function(e)
            {
                e.preventDefault();
                var id = $(this).data("id");
                //console.log (id); 
                $.ajax({
                type : "post",
                url : "register.php",
                //data :  id,
                data:{'id':id},//values to be passed similarly
                cache : false,
                success : function(html)
                {
                    $('#msg').html(html);
                }});
  });
});
</script>

像這樣更改$.ajax()函數:

$.ajax({
    type: 'POST',
    url: 'register.php',
    data: {
        id: id
    },
    success: function(response)
    {
        $('#msg').html(response);
    }
});

希望對您有幫助...

嘗試這個;

     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

     $.ajax({
              type:'GET',
              url:'data.php',
              data: {
                  "id": 123,
                  "name": "abc",
                  "email": "abc@gmail.com"
              },
              success: function(ccc){

                  alert(ccc); 
                  $("#result").html(ccc);
              }
          });

data.php

echo $id = $_GET['id'];
echo $name = $_GET['name'];
echo $email = $_GET['email'];

希望這能解決您的問題。

暫無
暫無

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

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